2014-05-18 33 views
1

我正在制作一款应该允许用户通过其Google帐户注册的应用程序。我想尽可能多地自动检索配置文件信息。我发现this very interesting example,这将允许我获得许多信息(请参阅该演示的第4步)。现在,我如何在android上使用它?我看到很多关于如何使用Oauth2(Account Manager)获取身份验证令牌的例子(example),但我不知道该从那里做些什么来完成这些调用并检索这些信息。在那个例子中,代码是在JavaScript中,我不知道如何将它正确地移植到Java ...
我已经完成了谷歌开发控制台注册的东西。
Oauth2和OpenID是否一样?如果不是,我必须使用其中一个还是另一个?Android - Oauth2,AccountManager和Google:检索配置文件数据

+1

[这是关于入门任务API的官方文档和Android上的OAuth 2.0](https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android) –

+1

[这是OAuth2和Open ID的区别](http:// stackoverflow .COM /问题/ 1087031 /什么最differen ce-between-openid-and-oauth) –

+0

谢谢,这些链接非常有用。现在,sinc ethe我链接的示例(第一个链接)使用OpenID,我必须使用它还是可以使用谷歌的Oauth2?我可以跳到示例的第3步,并使用Android的AccountManager中检索到的令牌吗? – nonzaprej

回答

3

好的,完成了。正如预期的那样,我发现了文档中的所有信息,并且使用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 
} 
1

既然您想允许用户通过他们的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,并且有效。

所以总之,仔细阅读这些文档,你会清楚如何完成你的应用程序。

+0

是的,我已经阅读了大部分文档,并且我找到了一些有用的文档:[one](https://developers.google.com/+/api/latest/people),[two](https: //developers.google.com/+/mobile/android/sign-in)。我觉得我接近这个解决方案。现在基本上这个问题是针对我自己的,当我成功时,我会用代码发布答案。 – nonzaprej

相关问题