我正在制作一款应该允许用户通过其Google帐户注册的应用程序。我想尽可能多地自动检索配置文件信息。我发现this very interesting example,这将允许我获得许多信息(请参阅该演示的第4步)。现在,我如何在android上使用它?我看到很多关于如何使用Oauth2(Account Manager)获取身份验证令牌的例子(example),但我不知道该从那里做些什么来完成这些调用并检索这些信息。在那个例子中,代码是在JavaScript中,我不知道如何将它正确地移植到Java ...
我已经完成了谷歌开发控制台注册的东西。
Oauth2和OpenID是否一样?如果不是,我必须使用其中一个还是另一个?Android - Oauth2,AccountManager和Google:检索配置文件数据
回答
好的,完成了。正如预期的那样,我发现了文档中的所有信息,并且使用Google的Oauth2 Playground帮助了解了要发送到https://www.googleapis.com/oauth2/v1/userinfo
以便接收配置文件数据。
最后,事实证明,我们不需要在谷歌的开发控制台中创建客户端ID来执行此操作。
现在,到代码。活动:
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": "[email protected]",
"link": "https://plus.google.com/133780085840848123456",
"given_name": "John",
"id": "133780085840848123456",
"verified_email": true
}
既然您想允许用户通过他们的Google帐户登录您的应用,您可以使用OpenID,and Google supports it。
注意:如果您提供“使用Google登录”功能,我们建议您使用using Google+ Sign-In。
如果您只是想代表用户在Google中获取usr的信息,您可以使用Oauth2。请参阅Google'a官方文档,我认为它们很详细,权威且易于相处。
作为this doc说:从ID令牌
5.Obtain用户信息
ID令牌被在底部64 通常编码的加密的签名JSON对象,这是至关重要的您验证一个ID令牌在使用之前,但由于您通过无中介HTTPS渠道直接与Google通信,并使用您的客户端密钥向Google进行身份验证,因此您可以确信,您收到的令牌确实来自Google,并且有效。
所以总之,仔细阅读这些文档,你会清楚如何完成你的应用程序。
是的,我已经阅读了大部分文档,并且我找到了一些有用的文档:[one](https://developers.google.com/+/api/latest/people),[two](https: //developers.google.com/+/mobile/android/sign-in)。我觉得我接近这个解决方案。现在基本上这个问题是针对我自己的,当我成功时,我会用代码发布答案。 – nonzaprej
- 1. OAuth2,从Google Analytics检索数据
- 2. Google AccountManager Android
- 3. oAuth2检索iOS7中的配置文件电子邮件
- 4. 使用android AccountManager获取OAuth2令牌?
- 5. 使用google oauth2配置文件信息设置$ _SERVER php变量
- 6. 如何从playservice检索Google Plus配置文件11.0.4
- 7. gosimple/oauth2和Google OAuth2
- 8. 使用google oauth2获取用户配置文件
- 9. Passport.js如何使用OAuth2策略获取配置文件数据?
- 10. Google+ .Net API - 获取身份验证并检索配置文件
- 11. 从Google Apps配置文件API检索照片
- 12. Google Health - 制作医生档案并检索配置文件 - JAVA
- 13. 从数据库检索配置文件 - Java
- 14. Android AccountManager authToken和OAuth
- 15. Spring Boot:从数据库检索配置
- 16. Android上的google oauth2
- 17. 检索Google即时数据
- 18. LinkedIn Javascript SDK检索配置文件
- 19. Android和MongoDB存储和检索数据
- 20. 使用omniauth-google-oauth2从auth哈希中检索first_name和last_name gem
- 21. Android AccountManager
- 22. Android MapActivity无法检索位置数据
- 23. Foxx oauth2配置
- 24. laravel检索数据库的配置包中的配置
- 25. 检索在Spring.NET中配置对象的配置文件路径
- 26. Android - AccountManager和登录经典
- 27. YouTube数据API(v3):YouTube的Google OAuth2授权是否上传检索?
- 28. 配置应用程序以同步和检索MBO数据
- 29. Android数据库存储和检索
- 30. Android商店和检索数据
[这是关于入门任务API的官方文档和Android上的OAuth 2.0](https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android) –
[这是OAuth2和Open ID的区别](http:// stackoverflow .COM /问题/ 1087031 /什么最differen ce-between-openid-and-oauth) –
谢谢,这些链接非常有用。现在,sinc ethe我链接的示例(第一个链接)使用OpenID,我必须使用它还是可以使用谷歌的Oauth2?我可以跳到示例的第3步,并使用Android的AccountManager中检索到的令牌吗? – nonzaprej