问题而获取使用Google+的API域名圈Google+的信息 [英] Issue while fetching Google+ Circle information using Google+ Domain API

查看:484
本文介绍了问题而获取使用Google+的API域名圈Google+的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个小型Web应用程序,其中我在Google+ API领域的整合。
我使用的OAuth2 authentication.I已经产生了我的web应用程序的CLIENT_ID和client_secret
从谷歌API控制台。
使用Google+的API域名的,我能够生成访问令牌。

I am developing a small web application wherein I am integrating with Google+ Domain API's. I am using OAuth2 authentication.I have generated client_id and client_secret for my web application from Google API console. Using Google+ Domain API's, I am able to generate the access token.


  1. 生成验证网址

  1. Generating authorization 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);


  • 授权后

  • After authorization

     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();
    


  • 抓取圈信息:

  • Fetching circle information:

    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://本地主机:8080 / DomainWebApp / oauth2callback,因为它是一个Web应用程序。
    有什么建议?

    Note: I have also enabled Google+ Domain API in my Google API Console. REDIRECT_URI ="http://localhost:8080/DomainWebApp/oauth2callback" since it's a web app. Any Suggestions?

    推荐答案

    要检查的第一件事是,应用程序正在代表谷歌Apps用户的通话。如果用户帐户是,例如,一个@gmail帐户,该请求将不被允许。借助Google+ API域名只适用于谷歌Apps域用户,只为自己域内的请​​求。

    The first thing to check is that the application is making the call on behalf of a Google Apps user. If the user account is, for example, an @gmail account, the request will not be allowed. The Google+ Domains API only works for Google Apps domain users, and only for requests within their domain.

    这篇关于问题而获取使用Google+的API域名圈Google+的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆