- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
SE 上已经有几个类似的问题,但我已经阅读了所有我能找到的似乎相关的内容,但我仍然没有完全理解。
我得到了一个验证码,所以现在我需要用它来交换访问 token 和刷新 token 。然而,Google 返回了非常不明确的错误“invalid_request”。这是我的代码:
private const string BaseAccessTokenUrl = "https://accounts.google.com/o/oauth2/token";
private const string ContentType = "application/x-www-form-urlencoded";
public static string GetRefreshToken(string clientId, string clientSecret, string authCode)
{
Dictionary<string, string> parameters = new Dictionary<string, string>
{
{ "code", authCode },
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "redirect_uri", "http://localhost" },
{ "grant_type", "authorization_code" }
};
string rawJson = WebUtilities.Post(BaseAccessTokenUrl, parameters, ContentType);
return rawJson; // TODO: Parse out the actual refresh token
}
我的 Post()
方法对参数键和值进行 URL 编码并将它们连接起来:
public static string Post(string uri, Dictionary<string, string> properties, string contentType = "application/x-www-form-urlencoded")
{
string content = String.Join("&", from kvp in properties select UrlEncode(kvp.Key) + "=" + UrlEncode(kvp.Value) );
return Post(uri, content);
}
双参数 Post()
方法只处理将内容转换为字节、添加内容长度等,然后返回响应的内容,即使它来自 WebException
。如果有任何兴趣,我可以将其包括在内。
授权码看起来是对的,和我见过的其他类似:62个字符,以“4/”开头。我从 the Google API Console 中仔细复制的客户端 ID、密码和重定向 URL .该应用已注册为“其他”应用,我正在从 Windows 计算机进行连接。
根据 this和 this post ,我试过不进行 URL 编码,没有任何变化。 The OAuth Playground表明 URL 编码是正确的。
根据 this post和 this one , 属性连接在一行上。
根据 this post ,我已经在授权请求中尝试了 approval_prompt=force
,但是新的授权代码并没有更好地工作。授权码会过期吗?通常,我会在几秒钟内使用新代码。
根据 the Google docs和 this post ,我正在使用内容类型“application/x-www-form-encoded”。
我的授权请求是针对范围“https://www.googleapis.com/auth/analytics.readonly”。
根据 this post , 参数中没有前导问号。
有一个 Google .NET OAuth 库,但我无法轻松使用它,如果有选择的话,大约 50,000 行代码超出了我想研究的范围.我更喜欢从头开始写一些干净的东西,而不是盲目地复制一堆库, cargo 崇拜风格。
最佳答案
找到了。用于请求 token 的 redirect_uri
需要与获取授权代码时使用的相匹配。这是我获取授权码的工作代码:
private const string BaseAuthorizationUrl = "https://accounts.google.com/o/oauth2/auth";
public string GetAuthorizationUrl(string clientId, IEnumerable<string> scopes)
{
var parameters = new Dictionary<string, string>
{
{ "response_type", "code" },
{ "client_id", clientId },
{ "redirect_uri", RedirectUrl },
{ "scope", String.Join(" ", scopes) },
{ "approval_prompt", "auto" }
};
return WebUtilities.BuildUrl(BaseAuthorizationUrl, parameters);
}
...这是我获取访问 token 和刷新 token 的代码:
private const string BaseAccessTokenUrl = "https://accounts.google.com/o/oauth2/token";
public void GetTokens(string clientId, string clientSecret, string authorizationCode, out string accessToken, out string refreshToken)
{
var parameters = new Dictionary<string, string>
{
{ "code", authorizationCode },
{ "redirect_uri", RedirectUrl }, // Must match that used when authorizing an app
{ "client_id", clientId },
{ "scope", String.Empty },
{ "client_secret", clientSecret },
{ "grant_type", "authorization_code" }
};
string rawJson = WebUtilities.Post(BaseAccessTokenUrl, parameters, "application/x-www-form-urlencoded");
dynamic parsedJson = JsonUtilities.DeserializeObject(rawJson);
accessToken = parsedJson.access_token;
refreshToken = parsedJson.refresh_token;
}
...这是获取新访问 token 的代码:
public string GetAccessToken(string clientId, string clientSecret, string refreshToken)
{
var parameters = new Dictionary<string, string>
{
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "refresh_token", refreshToken },
{ "grant_type", "refresh_token" }
};
string rawJson = WebUtilities.Post(BaseAccessTokenUrl, parameters, "application/x-www-form-urlencoded");
dynamic parsedJson = JsonUtilities.DeserializeObject(rawJson);
return parsedJson.access_token;
}
关于c# - 请求访问 token 时通过 OAuth 2、 "invalid_request"连接到 Google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883913/
我似乎找不到造成这种情况的原因。我一遍又一遍地检查该网址,但似乎找不到问题所在。 function setTime() { min = $("#minutes").val(); hour = $("s
我在尝试使用 google auth 提供程序时遇到奇怪的错误,它抛出 invalid_request const googleLogin = new firebase.auth.GoogleAuth
我在 iOS 应用程序中遇到了问题。 我必须获取全部 60 个 Google 位置。我创建了用于发送请求和解析结果的循环。在这个循环中,我创建了这样的 URL: if (nextPageToken !
我已经创建了一个带有应用客户端设置()的用户池和设置域,用于托管注册和登录Cognito本身中的页面。当我尝试使用类似于以下内容的网址时- https://myDomain.auth.us-east-
我有以下代码: if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position)
我已采取的步骤 使用 Google 的开发者控制台: 我已经创建了一个项目并为该项目创建了一个客户端。 我已激活 Youtube API 数据 我将回调设置为 http://localhost:300
在我的 Android 应用程序中,用户可以选择他想查看的地点的类别(咖啡馆、餐馆等)。 当我使用以下 url 时,我从带有 next_page_token 的 google-places-api 获
我有一个本地运行的基本 Jetty 服务器。如果我拉起 http://localhost/servlet1在桌面上的网络浏览器中,我能够看到正确的内容(基本 html 页面)。但是,当我在 Andro
代码如下: googleMapsAPICall = "https://maps.googleapis.com/maps/api/place/add/json?sensor=true&key=MY_KE
我在用户登录后尝试从应用程序文件夹中检索用户的完整文件名列表时遇到错误 400。 我正在使用本教程来设置 Dropbox SDK: https://www.dropbox.com/developers
我在 Appengine for Go 中使用拉队列,虽然本地租赁任务工作正常,但当我部署代码时,对 taskqueue.Lease 的调用给了我这个错误: API 错误 13(任务队列:INVALI
我想将我的应用与 Microsoft Graph 连接。我在 Azure 中创建了我的网络应用程序(我有我的 client_id 和 client_secret)。我可以发送请求以从 https://
在我使用完服务帐户后尝试撤销我的 gcloud 凭据时,我目前遇到了不一致的情况。 gcloud auth activate-service-account --key-file=mykey.json
我们使用 Google OAuth2 来验证我们的用户进入内部应用程序,使用 HybridAuth 2.4.0,直到大约一周前,我们开始看到越来越多来自 https://accounts.google
我尝试使用 Nuxt Auth 模块设置 google 身份验证,但我从 google 收到以下错误: Error 400 : invalid_request Parameter not allowe
我在使用 Adobe Sign 的 OAuth 身份验证时遇到问题——每次我尝试使用我的客户端 ID 和重定向 URI 获取访问 token 时,我都会收到以下消息: Unable to auth
我正在尝试为iOS的youtube应用获取访问 token 。这是我在viewDidLoad方法中一直使用的相关代码: mAuth = [[GTMOAuth2Authentication alloc]
我正在尝试使用以下 URL 获取应用程序的访问 token : https://datamarket.accesscontrol.windows.net/v2/OAuth2-13?grant_type
至少这个应该很容易让你验证。此请求返回 INVALID_REQUEST : https://maps.googleapis.com/maps/api/place/nearbysearch/json?l
问题:为什么 Google 返回 INVALID_REQUEST 响应?我猜这与请求的构造不正确有关。 (内容长度为“2”) public void addNewPlace(String latitu
我是一名优秀的程序员,十分优秀!