我试图在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()
我是什么失踪?
我花了一天收到此为谷歌协调中心API大约2周前的工作,遗憾的是我对这个代码是对我的工作的笔记本电脑更换的范围,直到星期四我都无法继续。我似乎记得这篇文章让我朝着正确的方向前进。 http://blog.tomtasche.at/2013/05/google-oauth-on-android-using.html祝你好运!我最喜欢这个问题,试图提醒我在有权访问代码时办理登机手续。 – Robadob
@Robadob谢谢你的文章,我会试试:) – DropDropped
确保你的作用域字符串的格式为'oauth2:https://www.googleapis.com/auth/glass.timeline https:// www。 googleapis.com/auth/userinfo.profile'我记得那是我的问题之一。 – Robadob