在OAuth2中获取访问令牌

时间:2013-07-29 21:14:25

标签: java android oauth-2.0 access-token google-oauth

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

首先,我已在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&#34;未知&#34;。如何正确获取用户代码?

编辑:

我终于可以使用以下方法获取身份验证令牌:

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

其中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()

我错过了什么?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

您应该用您的SCOPE替换,我认为应该是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;
相关问题