获取Google+时出现问题;使用谷歌+;域API

获取Google+时出现问题;使用谷歌+;域API,api,google-plus,Api,Google Plus,我正在开发一个小型web应用程序,其中我正在与Google+域API集成。 我正在使用OAuth2身份验证。我已经为我的web应用程序生成了客户端id和客户端机密 从谷歌API控制台。 使用Google+域API,我能够生成访问令牌 生成授权URL List<String> SCOPE = Arrays.asList( "https://www.googleapis.com/auth/plus.me", "https://www.googleapis.com/auth/plus.ci

我正在开发一个小型web应用程序,其中我正在与Google+域API集成。 我正在使用OAuth2身份验证。我已经为我的web应用程序生成了客户端id和客户端机密 从谷歌API控制台。 使用Google+域API,我能够生成访问令牌

  • 生成授权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);
    
    注意:我还在我的Google API控制台中启用了Google+域API。
    REDIRECT_URI=”http://localhost:8080/DomainWebApp/oauth2callback“
    因为它是一个web应用程序。
    有什么建议吗?

    首先要检查的是,应用程序正在代表谷歌应用程序用户进行呼叫。例如,如果用户帐户是@gmail帐户,则不允许该请求。Google+Domains API仅适用于Google Apps域用户,并且仅适用于其域内的请求。

    谢谢Joanna。此外,如果我希望同事访问我的web应用,我需要在重定向uri中指定我的计算机的ip地址。但是当我尝试在API控制台中将重定向uri修改为“无效uri”时.有没有办法修改重定向uri?
     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();
    
    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)