【问题标题】:OAuth2 Authentication for "Google Service" returns "invalid_request"“Google 服务”的 OAuth2 身份验证返回“invalid_request”
【发布时间】:2012-04-25 09:00:27
【问题描述】:

我正在尝试形成一个 json 请求,以使用 oath2 规范进行身份验证以进行 Google 的“服务帐户”身份验证。我正在使用谷歌的文档here。它正在使用 JWT。似乎没有太多关于如何使用 C# 执行此操作的信息。这是我正在使用的 RAW json 请求,但我能从谷歌得到的唯一响应是“invalid_request”。

POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: accounts.google.com
Content-Length: 457
Expect: 100-continue
Connection: Keep-Alive

{
  "grant_type": "assertion",
  "assertion_type": "http://oauth.net/grant_type/jwt/1.0/bearer",
  "assertion": "JWT (including signature)"
}

对可能发生的事情有任何想法吗?我正在尝试创建一个 Windows 服务,以设定的时间间隔 ping 谷歌纬度位置?是否有另一种方法可以在不跳过这个环节的情况下访问该 API?谢谢!

【问题讨论】:

  • 显然我正在用我自己计算的令牌替换“JWT(包括签名)”
  • 看起来不像符合“application/x-www-form-urlencoded”的内容。那是json。

标签: c# .net google-api oauth-2.0 jwt


【解决方案1】:

文档相对清楚,您应该发布一个 urlencoded 字符串。与其尝试发布 json,不如发布 application/x-www-form-urlencoded 数据:

var invariantPart = "grant_type=assertion&" + 
   "assertion_type=http%3A%2F%2Foauth.net%2Fgrant_type%2Fjwt%2F1.0%2Fbearer&" +
   "assertion=";
var fullPostData = invariantPart + Uri.EscapeDataString(myCalculatedAssertion);
//POST fullPostData to google

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 2015-09-16
    • 1970-01-01
    • 2016-01-02
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多