谷歌Android ACC:交换的authorization_ code为的access_token [英] android google acc: Exchange the authorization_code for an access_token
本文介绍了谷歌Android ACC:交换的authorization_ code为的access_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图交换AUTH code访问令牌,但没有成功。我需要访问的用户配置文件。
这里是我的code:
I tried to to exchange auth code for an access token, but no success. I need to access user profile. Here is my code:
先得到AUTH code:
first get auth code:
编辑:
private void googleAuthenticate(){
try {
mOAauthHelper = new OAuthHelper("something.net", "xxxxx",
"https://www.googleapis.com/auth/userinfo.profile", "alex://myScheme");
String uri = mOAauthHelper.getRequestToken();
startActivity(new Intent("android.intent.action.VIEW", Uri.parse(uri)));
//Intent i = new Intent(this, GoogleOAUTHActivity.class);
//i.putExtra(GoogleOAUTHActivity.GOOGLE_OAUTH_ENDPOINT_KEY, uri);
//startActivity(i);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
failedAuthenticatingAtGoogle();
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
failedAuthenticatingAtGoogle();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
failedAuthenticatingAtGoogle();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
failedAuthenticatingAtGoogle();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
failedAuthenticatingAtGoogle();
}
}
和
public OAuthHelper(String consumerKey, String consumerSecret, String scope,
String callbackUrl) throws UnsupportedEncodingException {
mConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
mProvider = new CommonsHttpOAuthProvider(
"https://www.google.com/accounts/OAuthGetRequestToken?scope="
+ URLEncoder.encode(scope, "utf-8"),
"https://www.google.com/accounts/OAuthGetAccessToken",
"https://www.google.com/accounts/OAuthAuthorizeToken?hd=default");
mProvider.setOAuth10a(true);
mCallbackUrl = (callbackUrl == null ? OAuth.OUT_OF_BAND : callbackUrl);
}
和
@Override
protected void onNewIntent(Intent intent) {
System.out.println("onNewIntent");
try {
Uri uri = intent.getData();
String oauthToken = uri.getQueryParameter("oauth_token");
// String oauthToken = uri.getQueryParameter("oauth_verifier");
System.out.println(oauthToken);
if(oauthToken != null){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("code", oauthToken));
pairs.add(new BasicNameValuePair("client_id", "something.net"));
pairs.add(new BasicNameValuePair("client_secret", "xxxxxx"));
pairs.add(new BasicNameValuePair("redirect_uri", "alex://myScheme"));
pairs.add(new BasicNameValuePair("grant_type", "authorization_code")); //Leave this line how it is
post.setEntity(new UrlEncodedFormEntity(pairs));
org.apache.http.HttpResponse response = client.execute(post);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody); // That just logs it into logCat
//authorizeGoogleSessionToServer(oauthToken);
}
} catch (Exception e) {
e.printStackTrace();
}
}
和我得到:
{
错误:INVALID_REQUEST
}
and I get: { "error" : "invalid_request" }
有谁知道如何解决这个问题呢?
Does anybody know how to solve this problem?
感谢。
推荐答案
您是的双编码的的 REDIRECT_URI
参数。
这应该是这个样子:
pairs.add(new BasicNameValuePair("redirect_uri", "your_own_redirect_URI"));
此外,你应该注意不要混协议(您好!OAuth1和OAuth2用户)的两个实现。他们看起来是一样的,但不是平等的。
Also, you should be careful to not mix both implementations of the protocol (OAuth1 and OAuth2). They look the same but are not equal.
这篇关于谷歌Android ACC:交换的authorization_ code为的access_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文