2017-04-21 29 views
1

我是android開發新手。我處於需要使用其Google帳戶登錄的用戶的性別和出生日期的情況。使用Oauth無法找到我的項目的服務器客戶端密鑰

對於此我使用的如下所示

class GetGendersTask extends AsyncTask<GoogleSignInAccount, Void, List<Gender>> { 
    @Override 
    protected List<Gender> doInBackground(GoogleSignInAccount... googleSignInAccounts) { 
     List<Gender> genderList = new ArrayList<>(); 
     try { 
      HttpTransport httpTransport = new NetHttpTransport(); 
      JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 

      //Redirect URL for web based applications. 
      // Can be empty too. 
      String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 

      // Exchange auth code for access token 
      GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
        httpTransport, 
        jsonFactory, 
        getApplicationContext().getString(R.string.server_client_id), 
        getApplicationContext().getString(R.string.server_client_secret), 
        googleSignInAccounts[0].getServerAuthCode(), 
        redirectUrl 
      ).execute(); 

      GoogleCredential credential = new GoogleCredential.Builder() 
        .setClientSecrets(
          getApplicationContext().getString(R.string.server_client_id), 
          getApplicationContext().getString(R.string.server_client_secret) 
        ) 
        .setTransport(httpTransport) 
        .setJsonFactory(jsonFactory) 
        .build(); 

      credential.setFromTokenResponse(tokenResponse); 

      People peopleService = new People.Builder(httpTransport, jsonFactory, credential) 
        .setApplicationName("My Application Name") 
        .build(); 

      // Get the user's profile 
      Person profile = peopleService.people().get("people/me").execute(); 
      genderList.addAll(profile.getGenders()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return genderList; 
    } 

    @Override 
    protected void onPostExecute(List<Gender> genders) { 
     super.onPostExecute(genders); 
     // iterate through the list of Genders to 
     // get the gender value (male, female, other) 
     for (Gender gender : genders) { 
      String genderValue = gender.getValue(); 
     } 
    } 
} 

爲了成功地執行該上述代碼的代碼,我需要的server_client_id和server_client_secret。進入我的應用程序的開發人員控制檯後,我發現我的server_client_id,但不幸的是我無法找到server_client_secret密鑰。 我衝浪了很多,並沒有找到答案。我知道很多人已經解決了這個問題。但遺憾的是,我無法找到答案。

任何人都可以幫助我解決這個問題。

在此先感謝。 :)

回答

0

Android不使用客戶端機密。由於它是用戶擁有的設備,因此長久以來不會是一個祕密。如果您的項目需要客戶端密鑰,請轉至開發控制檯並創建一組額外的憑據,並指定Web App。

+0

那麼有沒有其他的方式來獲得像性別和DOB的個人信息,而不使用這個服務器客戶端密鑰? –

+0

請參閱https://developers.google.com/+/web/api/rest/latest/people/get以及已經提出這個問題的許多關於SO的問題。例如http://stackoverflow.com/questions/7349747/unable-to-get-user-profile-from-google-using-google-apis-in-android – pinoyyid

+0

非常感謝:) –

相關問題