2014-01-31 4 views
2

Google+ 도메인 API와 통합되는 소규모 웹 애플리케이션을 개발 중입니다. OAuth2 인증을 사용 중입니다. Google API 콘솔의 웹 응용 프로그램 에 대한 client_id 및 client_secret가 생성되었습니다. Google+ 도메인 API를 사용하여 액세스 토큰을 생성 할 수 있습니다.Google+ 도메인 API를 사용하여 Google+ 서클 정보를 가져 오는 중에 문제가 발생했습니다.

  1. 인증 URL을 생성

    List<String> SCOPE = Arrays.asList(
    "https://www.googleapis.com/auth/plus.me", 
    "https://www.googleapis.com/auth/plus.circles.read", 
    "https://www.googleapis.com/auth/plus.stream.write"); 
    
    //Sets up Authorization COde flow 
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(new NetHttpTransport(), 
        new JacksonFactory(), 
        "xxx","yyy",SCOPE).setApprovalPrompt("force").setAccessType("offline").build(); 
    
    //Builds the uthorization URL 
    String url = flow.newAuthorizationUrl().setRedirectUri(<REDIRECT_URI>).build(); 
    out.println("<div id='googleplus'></div><a href='"+url+"' rel='external' ><img src='googleplus.jpg'></a> <b>Configure</b></div>"); 
    session.setAttribute("CodeFlow", flow); 
    
  2. 인증

    GoogleAuthorizationCodeFlow flow=(GoogleAuthorizationCodeFlow)session. getAttribute("CodeFlow"); 
    
    //After authorization,fetches the value of code parameter 
    String authorizationCode=request.getParameter("code"); 
    
    //Exchanges the authorization code to get the access token 
    GoogleTokenResponse tokenResponse=flow.newTokenRequest(authorizationCode). 
        setRedirectUri(<REDIRECT_URI>).execute(); 
    
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory()) 
    .setClientSecrets("xxx", "yyy") 
    .addRefreshListener(new CredentialRefreshListener(){ 
    
        public void onTokenErrorResponse(Credential credential, TokenErrorResponse errorResponse) throws java.io.IOException{ 
         System.out.println("Credential was not refreshed successfully. " 
           + "Redirect to error page or login screen."); 
    
        } 
    
    
        @Override 
        public void onTokenResponse(Credential credential, TokenResponse tokenResponse) 
          throws IOException { 
         System.out.println("Credential was refreshed successfully."); 
         System.out.println("Refresh Token :"+tokenResponse.getRefreshToken()); 
    
        } 
    }).build(); 
    
    //Set authorized credentials. 
    credential.setFromTokenResponse(tokenResponse); 
    credential.refreshToken(); 
    
  3. 가져 오기 원 정보 후 :

    PlusDomains plusDomains = new PlusDomains.Builder(
         new NetHttpTransport(), new JacksonFactory(), credential) 
         .setApplicationName("DomainWebApp") 
         .setRootUrl("https://www.googleapis.com/") 
         .build(); 
    PlusDomains.Circles.List listCircles=plusDomains.circles().list("me"); 
    listCircles.setMaxResults(5L); 
    System.out.println("Circle URL:"+listCircles.buildHttpRequestUrl()); 
    CircleFeed circleFeed=listCircles.execute(); 
    System.out.println("Circle feed:"+circleFeed); 
    List<Circle> circles =circleFeed.getItems(); 
    
    while (circles != null) { 
         for (Circle circle : circles) { 
          out.println("Circle name : "+circle.getDisplayName()+" Circle id : "+circle.getId()); 
         } 
    
         // When the next page token is null, there are no additional pages of 
         // results. If this is the case, break. 
         if (circleFeed.getNextPageToken() != null) { 
         // Prepare the next page of results 
         listCircles.setPageToken(circleFeed.getNextPageToken()); 
    
         // Execute and process the next page request 
         circleFeed = listCircles.execute(); 
         circles = circleFeed.getItems(); 
         } else { 
         circles = null; 
         } 
        } 
    
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
{ 
    "code" : 403, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Forbidden", 
    "reason" : "forbidden" 
    } ], 
    "message" : "Forbidden" 
} 
    com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) 
    com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 

참고 : 나는 아래의 오류가 나는 또한 내 Google API 콘솔의 Google+ 도메인 API를 사용할 수있다. 웹 앱이기 때문에 REDIRECT_URI ="http://localhost:8080/DomainWebApp/oauth2callback"입니다. 제안 사항이 있으십니까?

답변

2

먼저 확인해야 할 것은 애플리케이션이 Google Apps 사용자 대신 전화를 걸고 있다는 것입니다. 사용자 계정이 예를 들어 @ Gmail 계정 인 경우 요청이 허용되지 않습니다. Google+ 도메인 API는 Google Apps 도메인 사용자 및 도메인 내의 요청에 대해서만 작동합니다.

+0

Joanna.Also 내 동료가 내 웹 응용 프로그램에 액세스 할 수있게하려면 redirect_uri에서 내 컴퓨터의 IP 주소를 지정해야합니다. 그러나 http : //로 api 콘솔의 redirect_uri를 수정하려고합니다. /10.66.126.156:8080/DomainWebApp/oauth2callback, "invalid uri"라고 말합니다. redirect_uri를 수정할 수있는 방법이 있습니까? – suz

관련 문제