2016-06-13 80 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

相關問題