2014-01-31 30 views
2

我正在開發一個小型網絡應用程序,其中我正在與Google+域API進行集成。 我正在使用OAuth2身份驗證。我爲來自Google API控制檯的我的Web應用程序 生成了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域名在我的谷歌API控制檯。 REDIRECT_URI ="http://localhost:8080/DomainWebApp/oauth2callback"因爲它是一個網絡應用程序。 有何建議?

回答

2

首先要檢查的是應用程序代表Google Apps用戶撥打電話。例如,如果用戶帳戶是@gmail帳戶,則該請求將不被允許。 Google+ Domains API僅適用於Google Apps域用戶,並且僅適用於其域中的請求。

+0

感謝Joanna.Also如果我想讓我的Web應用程序被我的同事訪問,我需要在redirect_uri中指定我的機器的IP地址。但是當我嘗試將api控制檯中的redirect_uri修改爲http:/ /10.66.126.156:8080/DomainWebApp/oauth2callback,it說「無效的uri」。是否有任何方法可以修改redirect_uri? – suz

相關問題