2014-04-14 95 views
2

我需要获取访问令牌并将其发送到服务器。使用该访问令牌服务器应获取所有用户详细信息,如姓名,个人资料图片和电子邮件。Google plus登录访问令牌retriew

我可以使用Scopes.PLUS_LOGINScopes.PLUS_ME访问令牌,但与访问令牌服务器无法获得用户的电子邮件。

这里是我的代码:

@Override 
public void onConnected(Bundle arg0) { 
    mSignInClicked = false; 


    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 
      String token = null; 
      String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME; 
      try { 
       token = GoogleAuthUtil.getToken(
         getApplicationContext(), 
         Plus.AccountApi.getAccountName(mGoogleApiClient), 
         scope); 
       appUser.setToken(token); 
      } catch (IOException transientEx) { 
       // Network or server error, try later 
       Log.e(TAG, transientEx.toString()); 
      } catch (UserRecoverableAuthException e) { 
       // Recover (with e.getIntent())     
      } catch (GoogleAuthException authEx) { 
       // The call is not ever expected to succeed 
       // assuming you have already verified that 
       // Google Play services is installed. 
       Log.e(TAG, authEx.toString()); 
      } 

      return token; 
     } 

     @Override 
     protected void onPostExecute(String token) { 
      Log.i(TAG, "Access token retrieved:" + appUser.getToken()); 
      // Get user's information 
     } 

    }; 


} 

是否有人知道如何解决这个问题?

+0

我想使用您的代码。你能解释我什么是appUser和setToken()吗? –

+0

它是POJO类和二传手和吸气剂。你可以保存没有的结果。 – Zookey

回答

2

你缺少范围

https://www.googleapis.com/auth/userinfo.email 

我测试的其他范围内,只有一个看似回报用户的电子邮件。您可以测试不同的范围和他们返回的内容:People: get

注意:我不是一个android程序员,你可能会有更好的运气找出如何用android请求该范围。我正在寻找,但一直未能找到它。

看起来像范围可能只是电子邮件https://developers.google.com/+/api/oauth#email

+0

我的最终范围应该怎样看? String scope =“oauth2:”+ Scopes.PLUS_LOGIN +“”+ Scopes.PLUS_ME +“https://www.googleapis.com/auth/userinfo.email”; – Zookey

+0

这可能不会起作用。你需要找到适用于android的正确范围。这就是api使用的实际范围。 – DaImTo

+0

看看是否有一个名为Scopes.email或类似 – DaImTo