我正在制作一个应允许用户通过其谷歌帐户注册的应用程序。我想尽可能多地自动检索个人资料信息。我找到了 this very interesting example ,这将使我获得许多信息(请参阅该演示的第 4 步)。现在,我如何在安卓上使用它?我看到了许多关于如何使用 Oauth2 ( example ) 通过 AccountManager 获取身份验证 token 的示例,但我不知道如何从那里进行调用和检索这些信息。同样在那个例子中,代码是在 javascript 中,我不知道如何将它正确地移植到 java...
我已经完成了谷歌开发控制台注册工作。
Oauth2 和 OpenID 是一回事吗?如果不是,我必须使用其中之一吗?
最佳答案
好的,完成了。正如预期的那样,我在文档中找到了所有信息,并且使用 Google 的 Oauth2 Playground 帮助了解发送到 https://www.googleapis.com/oauth2/v1/userinfo
的内容以接收配置文件数据。
最后,事实证明我们不需要在谷歌的开发控制台中创建客户端 ID 来执行此操作。
现在,到代码。 Activity :
public class MainActivity extends Activity {
public Activity mContext;
private AccountManager accountManager;
private final String SCOPES = "oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
private String authToken;
private GetProfileDataTask googleTask;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mContext = this;
accountManager = AccountManager.get(mContext);
//other stuff here...
}
public void getProfileData() {
accountManager.getAuthTokenByFeatures(
"com.google",
SCOPES,
null, mContext, null, null,
new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
//bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
//bundle.getString(AccountManager.KEY_ACCOUNT_TYPE);
authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
} catch (Exception e) {
System.out.println("getAuthTokenByFeatures() cancelled or failed:");
e.printStackTrace();
authToken = "failure";
}
if(!authToken.equals("failure")) {
googleTask = new GetProfileDataTask();
googleTask.execute(authToken);
}
}
}, null);
}
}
获取数据的AsyncTask:
public class GetProfileDataTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... tokens) {
RestTemplate restTemplate = new RestTemplate(false);
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
String json = null;
try {
//the response is of type "application/json"
json = restTemplate.getForObject(
"https://www.googleapis.com/oauth2/v1/userinfo" +
"?access_token={token}" +
"&access_token_type=bearer",
String.class,
tokens[0]); //this is the authToken from before, obv
} catch(RestClientException er) {
Log.e("GetProfileDataTask", er.toString(), er);
json = null;
}
return json;
}
@Override
protected void onPostExecute(String asyncResult) {
if(asyncResult != null)
//do something with your data, for example deserialize it
else
//do something else
}
}
收到的json是这样的:
{
"family_name": "Smith",
"name": "John Smith",
"picture": "https://lh3.googleusercontent.com/-randomlettersandnumbers/AAAAAAAAAAI/AAAAAAAAAAA/morerandomlettersandnumbers/photo.jpg",
"locale": "it",
"gender": "male",
"email": "youremail@whatever.itis",
"link": "https://plus.google.com/133780085840848123456",
"given_name": "John",
"id": "133780085840848123456",
"verified_email": true
}
关于Android - Oauth2、AccountManager 和 Google : retrieve profile data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23725448/