c# - 用于服务应用程序的 Google oAuth 2.0(JWT token 请求)

标签 c# oauth oauth-2.0 google-oauth jwt

我正在尝试为此处描述的服务帐户实现 Google oAuth 2:https://developers.google.com/accounts/docs/OAuth2ServiceAccount在 UnityScript(或 C# - 这并不重要,因为它们都使用相同的 Mono .NET 类)上。

我在这里找到了类似的主题:Is there a JSON Web Token (JWT) example in C#? web-token-jwt-example-in-c 但我仍然没有成功。

首先,我已经生成了 header 和声明集(就像在谷歌文档中一样)

var header: String = GetJWTHeader();
var claimset: String = GetJWTClaimSet();

结果是(为清楚起见用新行分隔):

{"alg":"RS256","typ":"JWT"}

{"iss":"425466719070-1dg2rebp0a8fn9l02k9ntr6u5o4a8lp2.apps.googleusercontent.com",

"scope":"https://www.googleapis.com/auth/prediction",

"aud":"https://accounts.google.com/o/oauth2/token",

"exp":1340222315,

"iat":1340218715}

Base-64编码方式:

public static function Base64Encode(b: byte[]): String {
    var s: String = Convert.ToBase64String(b);
    s = s.Replace("+", "-");
    s = s.Replace("/", "_");
    s = s.Split("="[0])[0]; // Remove any trailing '='s
    return s;
}

public static function Base64Encode(s: String): String {    
    return Base64Encode(Encoding.UTF8.GetBytes(s));
}

那我要签名了。

var to_sign: byte[] = 
     Encoding.UTF8.GetBytes(Base64Encode(header) + "." + Base64Encode(claimset));
var cert: X509Certificate2 = 
     new X509Certificate2(google_pvt_key.ToArray(), "notasecret");
var rsa: RSACryptoServiceProvider = cert.PrivateKey;
var sgn: String = Base64Encode(rsa.SignData(to_sign, "SHA256"));

var jwt: String = Base64Encode(header) + "." + Base64Encode(claimset) + 
                     "." + sgn;

然后形成请求:

var url: String = "https://accounts.google.com/o/oauth2/token";
var form: WWWForm = new WWWForm();
form.AddField("grant_type", "assertion");
form.AddField("assertion_type", "http://oauth.net/grant_type/jwt/1.0/bearer");
form.AddField("assertion", jwt);
var headers: Hashtable = form.headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";

var www: WWW = new WWW(url, form.data, headers);

我得到的只是“错误 400:错误请求”。

编码后的数据看起来像(为清楚起见添加了换行符):

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.

eyJpc3MiOiI0MjU0NjY3MTkwNzAtMWRnMnJlYnAwYThmbjlsMDJrOW50cjZ1NW80YThscDIuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9vL29hdXRoMi90b2tlbiIsImV4cCI6MTM0MDIyMjMxNSwiaWF0IjoxMzQwMjE4NzE1fQ.

lIFg7-Og_BcC5qpICLt7USwGIHUOz-vV4ADNq0AWhuRtsvFrbZn5mxk4n9r5qU66q4reTVVAtuW06DeGsdcBMNgEdIMvN6VuYQybs64p9mqrfECBYxO1FWHbUG-2On1IpowybEsRRUjZfp0jFuEY7SLE3XRaXan0k5zmejcvLQo

我花了两天时间试图找出问题所在,但我看不到。

此外,我找不到任何合适的文档和示例。

我只是想收到一个 token 。

  1. 我是否以正确的方式对字节进行签名?
  2. claimset 中的“scope”参数应该是什么样的?我试过“https://www.googleapis.com/auth/devstorage.readonly”和“https://www.googleapis.com/auth/prediction”。
  3. “iss”参数应该等于什么?客户 ID 或电子邮件地址? (都试过了)
  4. 有哪些方法可以找出我的错误?
  5. 是否有任何用于服务应用程序的 C# 库(不适用于已安装的应用程序或客户端登录)?

我快疯了...它必须工作,但它不会...:-/

最佳答案

解决方案是在请求代码中所有斜杠都必须反斜杠

错误:

"scope":"https://www.googleapis.com/auth/prediction",
"aud":"https://accounts.google.com/o/oauth2/token",

正确:

"scope":"https:\\/\\/www.googleapis.com\\/auth\\/prediction",
"aud":"https:\\/\\/accounts.google.com\\/o\\/oauth2\\/token",

关于c# - 用于服务应用程序的 Google oAuth 2.0(JWT token 请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126439/

相关文章:

c# - 登录和启动画面

javascript - Firebase JS v9 中 Google Auth Provider 的 select_account 提示

javascript - 使用 PHP 登录和 OAuth2

google-apps-script - 刷新 token "error": "invalid_client" on google app script - new feature upgrade auth

c# - 了解 BackgroundWorker

c# - 一次保存多个实体

c# - 在静态方法中返回 'this'

Spring Security OAuth2 accessToken

安卓 & OAUTH 2.0

asp.net - Facebook 身份验证与 ASP.NET Core 2.1 : OAuth "Facebook Platform" "invalid_code" "This authorization code has been used."