2013-07-29 107 views
0

我试图在Java设备上获取访问令牌。在OAuth2中获取访问令牌

起初,我在Google API Console中为安装的应用程序(Android)创建了客户端ID,并设置了访问令牌将被请求的包以及SHA1 fingerprint

正如我在OAuth2ForDevices中看到的,为了获得访问令牌,我必须先获得一个用户代码。于是,我就用Aquery这样的client_id和范围发送POST请求:

AQuery aq = new AQuery(activity); 
     aq.ajax("https://accounts.google.com/o/oauth2/device/code?" + 
       "scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile" + 
       "client_id=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", 
      JSONObject.class,new AjaxCallback<JSONObject>(){ 

      @Override 
      public void callback(String url, JSONObject traffic_flow, AjaxStatus status) { 

       publishProgress(traffic_flow.toString()); 

      } 
    }); 

的问题是,JSONObject traffic_flow是百达空。我也尝试过用这个来获得(但是我没有事情,这是一个正确的方式):

authToken = GoogleAuthUtil.getToken(activity, mEmail, "audience:server:client_id:xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"); 

其中mEmail是从Android设备的电子邮件,但我得到了GoogleAuthException“未知”。我如何正确获取用户代码?

编辑:

我终于能使用该获得的身份验证令牌:

String scope = "audience:server:client_id:xxxxxxxxxxxx.apps.googleusercontent.com"; 
String token = GoogleAuthUtil.getToken(activity, client.getAccountName(), scope); 

其中范围是为其在生成Google API Console web应用程序客户端ID(事后我发送令牌到我的网站并验证它),客户端是PlusClient(更多在Getting started with Google+)。

我获得的令牌在我的测试应用,现在的问题是,当我想包括代码到我的新的应用程序,我又得到那个丑陋的例外:

com.google.android.gms.auth.GoogleAuthException: Unknown at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 

所有这些应用程序的客户端ID在同一个项目中,清单中的权限应该是正确的(GET_ACCOUNTS,USE_CREDENTIALS,AUTHENTICATE_ACCOUNTS,INTERNET,ACCESS_NETWORK_STATE)。创建PlusClient时,因为它不无它的工作,我在新的应用程序所做的唯一变化是设置范围(不知道为什么它的工作原理没有它在我的测试应用程序)

mPlusClient = new PlusClient.Builder(this, this, this) 
      .setVisibleActivities("http://schemas.google.com/AddActivity") 
      .setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE) 
      .build() 

我是什么失踪?

+0

我花了一天收到此为谷歌协调中心API大约2周前的工作,遗憾的是我对这个代码是对我的工作的笔记本电脑更换的范围,直到星期四我都无法继续。我似乎记得这篇文章让我朝着正确的方向前进。 http://blog.tomtasche.at/2013/05/google-oauth-on-android-using.html祝你好运!我最喜欢这个问题,试图提醒我在有权访问代码时办理登机手续。 – Robadob

+0

@Robadob谢谢你的文章,我会试试:) – DropDropped

+0

确保你的作用域字符串的格式为'oauth2:https://www.googleapis.com/auth/glass.timeline https:// www。 googleapis.com/auth/userinfo.profile'我记得那是我的问题之一。 – Robadob

回答

1

如果你的使用这个API,那么你应该使用POST版本的AQuery并正确地传递POST参数如下。此API OAuth2ForDevices适用于资源受限的设备,用户可以通过其他方式授权您的应用。

params.put("scope", "your scopes"); 
params.put("client_id", "your client id"); 

AQuery aq = new AQuery(activity); 
aq.ajax("https://accounts.google.com/o/oauth2/device/code", params, JSONObject.class, 
    new AjaxCallback<JSONObject>() { 
      @Override 
      public void callback(String url, JSONObject traffic_flow, AjaxStatus status) { 
       publishProgress(traffic_flow.toString()); 
      } 
}); 

但是,如果你的要求是在说Android手机使用普通的OAuth2与Android定期输入功能则与Android标准的OAuth机制this

+0

我想要做的是发送一个请求到我的网站,使用访问令牌,然后我可以验证它使用谷歌公钥,并发送回应。我尝试了你的方法,即使使用了[OAuth2InstalledApp](https://developers.google.com/accounts/docs/OAuth2InstalledApp),其中有一些附加参数,但我总是会收到网络错误和空响应。我可以使用您编写的链接登录,但可以使用它获取访问代码吗? – DropDropped

+0

可以请你帮我解决这个问题http://stackoverflow.com/questions/21445265/google-coordinate-authentification –

1

这是我用于获取OAuth 2.0令牌以用于Google Coordinate API的代码,我在ASyncTask中运行它。我没有包含用于生成DESIRED_USER的部分,因为它在某些AccountManager意图上使用startActivityForResult()。希望你可以使用这个。

你应该和你的,我认为应该是oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile

import android.accounts.Account; 
import android.accounts.AccountManager; 

final String SCOPE = "oauth2:https://www.googleapis.com/auth/coordinate"; 
AccountManager accountManager = AccountManager.get(context); 
//Kill Current token 
accountManager.invalidateAuthToken("com.google",authManager.getToken()); 
//Fetch userAccount 
Account userAccount = null; 
for(Account account : accountManager.getAccounts()) 
    if(account.name.equals(DESIRED_USER)) 
    { 
     try { 
      return accountManager.blockingGetAuthToken(userAccount, SCOPE, false); 
     } catch (Exception e) { 
      System.out.println("Error: "+e.toString()); 
      e.printStackTrace(); 
     } 
    } 
return null; 
+0

谢谢你的回复,不幸的是我无法以这种方式获得令牌 - 我收到了当使用'accountManager.blockingGetAuthToken(account,“oauth2:https://www.googleapis.com/auth/userinfo.email”,false);' – DropDropped

+0

文档说,返回 为此指定类型的验证令牌帐户,如果身份验证失败或无法获取,则为空。也许你使用的帐户没有G +?否则,我会期待它抛出一个异常。 “http://developer.android.com/reference/android/accounts/AccountManager.html#blockingGetAuthToken(android.accounts.Account,java.lang.String,boolean) – Robadob

+0

从何时开始GoogleAuthUtil.getToken()方法开始投掷对于未在设备上注册的电子邮件,而不是以前的异常IllegalArgumentException,带有消息“非现有帐户”email_address'“的消息为”BadUsername“的GoogleAuthException? http://developer.android.com/reference/com/ google/android/gms/auth/GoogleAuthUtil.html#getToken(android.content.Context,java.lang.String,java.lang.String) – toobsco42

相关问题