谷歌的OAuth 2.0(JWT令牌请求)的服务应用程序(Google oAuth 2.0 (JWT

2019-06-25 15:17发布

我试图实现谷歌的OAuth 2此处描述的服务帐户: https://developers.google.com/accounts/docs/OAuth2ServiceAccount上UnityScript(或C# -这并不重要,因为它们都使用相同的单声道.NET类)。

我在这里发现了类似的话题: 有没有在C#中的JSON网络令牌(JWT)的例子吗? 网络令牌智威汤逊 - 例如,在-C,但我仍然没有成功。

所有的拳,我已经生成的头和claimset(即只是像谷歌文档)

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

其结果(分离为清楚起见新行):

{ “ALG”: “RS256”, “典型”: “智威汤逊”}

{ “ISS”: “425466719070-1dg2rebp0a8fn9l02k9ntr6u5o4a8lp2.apps.googleusercontent.com”

“范围”:“ https://www.googleapis.com/auth/prediction ”

“AUD”: “ https://accounts.google.com/o/oauth2/token ”

“EXP”:1340222315,

“IAT”:1340218715}

基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

我花了两天时间试图弄清楚什么是错的,但我看不到。

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

我想刚刚收到的令牌。

  1. 我报名字节的正确方法?
  2. 应claimset“范围”参数是什么样的? 我已经试过“ https://www.googleapis.com/auth/devstorage.readonly ”和“ https://www.googleapis.com/auth/prediction ”。
  3. 什么“国际空间站”的参数应该等于? 客户端ID或邮件地址? (都试过)
  4. 什么是找出我的错误的方法呢?
  5. 是否有服务应用程序的任何C#库(不适用于安装的应用程序或客户端登录)?

我越来越疯狂......它的工作,但它不...: - /

Answer 1:

解决的办法是,在请求代码的所有斜杠必须反斜线

错误:

"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",


Answer 2:

我已经回答了类似的问题,以非常简单的,但工作实现与.NET谷歌的OAuth API的提议这里



Answer 3:

尝试使用system.identitymodel.tokens.jwt NET库可以从的NuGet安装。 见在我这里后例子(我用它来验证WebSockets的服务器,但处理JWT是通用的)

http://mydevtricks.blogspot.co.il/2014/10/xsocketsnet-authentication-with-jwt.html



文章来源: Google oAuth 2.0 (JWT token request) for Service Application