gpt4 book ai didi

Android - 如何获取 google plus 访问 token ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:02 30 4
gpt4 key购买 nike

您好,我在不使用带范围的 OAuth 2.0 客户端 ID 的情况下获取 google plus 访问 token 。但是使用此访问 token 不会获取电子邮件地址。如何获取用户邮箱地址?

使用和不使用 OAuth 2.0 客户端 ID 的 accesstoken 之间有什么区别吗?

我使用了下面的代码,

String accessToken="";
try {
accessToken = GoogleAuthUtil.getToken(
getApplicationContext(),
mPlusClient.getAccountName(), "oauth2:"
+ Scopes.PLUS_LOGIN + " "
+ Scopes.PLUS_PROFILE);

System.out.println("Access token==" + accessToken);
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

有两种简单的方法可以从 Google plus 获取用户电子邮件,

1.通过Plus.AccountApi.getAccountName如下所示,

String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

2.通过plus.profile.emails.read scope and REST end point如下所示,

获取 GooglePlus AccessToken

您需要通过 " https://www.googleapis.com/auth/plus.profile.emails.read"这个范围得到 AccessToken来自 GooglePlus,如下所示,

accessToken = GoogleAuthUtil.getToken(
getApplicationContext(),
mPlusClient.getAccountName(), "oauth2:"
+ Scopes.PLUS_LOGIN + " "
+ Scopes.PLUS_PROFILE+" https://www.googleapis.com/auth/plus.profile.emails.read");

对端点进行 REST 调用并进行简单的 JSON 解析

https://www.googleapis.com/plus/v1/people/me?access_token=XXXXXXXXXXXXX

您必须声明权限 <uses-permission android:name="android.permission.GET_ACCOUNTS" />在你的AndroidManifest.xml使用这些方法。

来自 Google 开发者网站的完整示例,

执行类似下面的操作以从 Google plus 检索经过身份验证的用户的电子邮件,

class UserInfo {
String id;
String email;
String verified_email;
}

final String account = Plus.AccountApi.getAccountName(mGoogleApiClient);

AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {

@Override
protected UserInfo doInBackground(Void... params) {
HttpURLConnection urlConnection = null;

try {
URL url = new URL("https://www.googleapis.com/plus/v1/people/me");
String sAccessToken = GoogleAuthUtil.getToken(EmailTest.this, account,
"oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read");

urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Bearer " + sAccessToken);

String content = CharStreams.toString(new InputStreamReader(urlConnection.getInputStream(),
Charsets.UTF_8));

if (!TextUtils.isEmpty(content)) {
JSONArray emailArray = new JSONObject(content).getJSONArray("emails");

for (int i = 0; i < emailArray.length; i++) {
JSONObject obj = (JSONObject)emailArray.get(i);

// Find and return the primary email associated with the account
if (obj.getString("type") == "account") {
return obj.getString("value");
}
}
}
} catch (UserRecoverableAuthException userAuthEx) {
// Start the user recoverable action using the intent returned by
// getIntent()
startActivityForResult(userAuthEx.getIntent(), RC_SIGN_IN);
return;
} catch (Exception e) {
// Handle error
// e.printStackTrace(); // Uncomment if needed during debugging.
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}

return null;
}

@Override
protected void onPostExecute(String info) {
// Store or use the user's email address
}

};

task.execute();

有关更多信息,请阅读此内容

https://developers.google.com/+/mobile/android/people

关于Android - 如何获取 google plus 访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23759529/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com