我是Android开发的新手。我处于需要使用其Google帐户登录的用户的性别和出生日期的情况。
为此,我使用的代码如下所示
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 :(得分:0)
Android不使用客户端机密。由于它是用户持有的设备,因此长期不会是秘密。如果您需要项目的客户机密,请转到开发控制台并创建一组额外的凭据,指定Web App。