2016-06-13 35 views
0

我想使用谷歌的人api,但我面临着很多困难。从api控制台生成的json文件不包含clientSecret。我在哪里可以得到它?所以我得到空授权代码。任何人都可以帮助我解决这个问题吗?在此先感谢Google People API在Android应用程序中的实现

public void setUp() throws IOException { 
      HttpTransport httpTransport = new NetHttpTransport(); 
      JacksonFactory jsonFactory = new JacksonFactory(); 

      // Go to the Google Developers Console, open your application's 
      // credentials page, and copy the client ID and client secret. 
      // Then paste them into the following code. 
      String clientId = ""; 
      String clientSecret = ""; 

      // Or your redirect URL for web based applications. 
      String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 
      String scope = "https://www.googleapis.com/auth/contacts.readonly"; 

      // Step 1: Authorize --> 
      String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId, 
        redirectUrl, 
        Arrays.asList(scope)) 
        .build(); 

      // Point or redirect your user to the authorizationUrl. 
      System.out.println("Go to the following link in your browser:"); 
      System.out.println(authorizationUrl); 

      // Read the authorization code from the standard input stream. 
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.println("What is the authorization code?"); 
      String code = in.readLine(); 

      System.out.println(code); 
      // End of Step 1 <-- 

      // Step 2: Exchange --> 
      GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
        httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute(); 
      // End of Step 2 <-- 

      GoogleCredential credential = new GoogleCredential.Builder() 
        .setTransport(httpTransport) 
        .setJsonFactory(jsonFactory) 
        .setClientSecrets(clientId, clientSecret) 
        .build() 
        .setFromTokenResponse(tokenResponse); 

      People peopleService = new People.Builder(httpTransport, jsonFactory, credential) 
        .build(); 

     } 
+0

你在哪里设置'clientSecrect'变量? – mikeyq6

+0

您是否阅读过上述变量的注释,//转到Google Developers Console,打开应用程序的凭证页面,然后复制客户端ID和客户端密钥。 //然后将它们粘贴到以下代码中。检查你的谷歌控制台。 –

+0

对不起卡尔帕纳,但我找不到clientSecrect那里。我只有clientId。 –

回答

-1

当您创建客户端ID,选择应用程序类型为“其他”,那么你可以找到clientSecret。 Screen shot

+0

我认为我们必须重新下载json文件。 –

1

要获得ClientID和ClientSecret,您应该创建Web应用程序ClientID并且有您需要的。

另一件需要注意的事情是response_type来自GoogleBrowserClientRequestUrl生成的网址将是token这会导致错误。我尝试更改response_typecode,它的工作原理。

完整的URL看起来像

https://accounts.google.com/o/oauth2/auth?client_id=YOUR_ANDROID_APPLICATION_CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/contacts.readonly

相关问题