适用于服务应用程序的Google oAuth 2.0(JWT令牌请求)
我试图在此处描述的服务帐户实施Google oAuth 2:UnityScript上的https://developers.google.com/accounts/docs/OAuth2ServiceAccount(或C# - 这并不重要,因为它们都使用相同的Mono .NET类)。适用于服务应用程序的Google oAuth 2.0(JWT令牌请求)
我在这里找到了类似的主题:Is there a JSON Web Token (JWT) example in C#? web-token-jwt-example-in-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));
}
然后我在做一个签名URE。
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
我花了两天时间试图弄清楚什么是错的,但我看不到。
此外,我找不到任何合适的文档和示例。
我只是想接收一个令牌。
- 我是否正确地签署了字节?
- 声明集中的“范围”参数应该是什么样子?我试过“https://www.googleapis.com/auth/devstorage.readonly”和“https://www.googleapis.com/auth/prediction”。
- 什么“iss”参数应该等于?客户ID或电子邮件地址? (两者都试过)
- 有什么方法可以找出我的错误?
- 服务应用程序是否有任何C#库(不适用于已安装的应用程序或客户端登录)?
我越来越疯狂......它的工作,但它不...: -/
的解决方案是,在请求代码的所有斜杠必须反斜线
WRONG:
"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",
soulburner你是我的英雄!非常感谢!我希望我能够upvote 10次:D –
尝试到u se system.identitymodel.tokens.jwt .Net库 您可以从NuGet安装它。 见例如在我的岗位在这里(我用它来验证WebSockets的服务器,但处理JWT是通用)
http://mydevtricks.blogspot.co.il/2014/10/xsocketsnet-authentication-with-jwt.html
我在C#中实现服务帐户身份验证时也很困难!你能否证实你成功地使用了grant_type和assertion_type字段,就像它们在你的例子中显示的那样?谢谢 – Nick