gpt4 book ai didi

android - Oauth 2 无法为访问 token 交换代码,返回 "invalid_grant"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:05 24 4
gpt4 key购买 nike

我发现当我的重定向 uri 是“urn:ietf:wg:oauth:2.0:oob”时,我可以通过 OAuth 2.0 进行身份验证,但是用户被迫复制代码,然后返回一个 Activity 并粘贴它变成了一个领域。我希望体验比这更优雅。当重定向 uri 为“http://localhost”时,(即使返回了访问代码)我无法将其交换为 api 的访问 token 。这是我的兑换码:

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
0);
nameValuePairs.add(new BasicNameValuePair("client_id",
OAuth2ClientCredentialsMark1.CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret",
OAuth2ClientCredentialsMark1.CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("code", accessCode));
nameValuePairs.add(new BasicNameValuePair("grant_type",
"authorization_code"));
nameValuePairs.add(new BasicNameValuePair("redirect_uri",
OAuth2ClientCredentialsMark1.REDIRECT_URI));
//"http://localhost"

String url = "https://accounts.google.com/o/oauth2/token";
//url += URLEncodedUtils.format(nameValuePairs, "utf-8");

Log.d("print", url);
HttpPost hPost = new HttpPost(
url);
hPost.setHeader("content-type", "application/x-www-form-urlencoded");

hPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

此代码总是返回 {"error": "invalid_grant"} 给出了什么?

我的应用程序基于示例 @ https://github.com/ddewaele/LatitudeOAuth2Sample我一直在关注教程 @ http://code.google.com/apis/accounts/docs/OAuth2InstalledApp.html

最佳答案

看来你的代码是正确的。错误来自 OAuth2 spec section-5.2 .

The provided authorization grant (e.g. authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.

很可能,您的应用尚未获得用户授权


回答您的问题:

the user is forced to copy the code, then go back one activity and paste it into a field

你能澄清一下是什么类型的oauth2 flow (scenario)吗? ,你在开发吗?

关于android - Oauth 2 无法为访问 token 交换代码,返回 "invalid_grant",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9336501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com