This walk through/demonstration and instruction for requesting a token does not work. When I execute my code exactly as you have it and run it, I receive a 400 error every time, and this json response:
{
error: "invalid_grant"
}
https://developers.google.com/accounts/docs/OAuth2ServiceAccount#makingrequest
I have been trying now to get this to work for almost a week, and am not getting any useful help here, and I see lot of similar questions here unanswered.
Thanks, any help would be amazing!
Karl..
Here is the code I am using (which I've wrapped up a bit and I may need to reveal internal code). Note: I left in the strange \/ slashes ins the scope and aud props of the claim as I am trying another guys fix from Stack http://goo.gl/bt9lPj (that doesn't seem to be working either and I'm getting the exact same error)
var claimbuilder = new Stub.Jwt.ClaimsBuilder();
claimbuilder.Add("iss", "...@developer.gserviceaccount.com");
claimbuilder.Add("scope", "https:\\/\\/picasaweb.google.com\\/data\\/");
claimbuilder.Add("aud", "https:\\/\\/accounts.google.com\\/o\\/oauth2\\/token");
claimbuilder.Add("exp", (Stub.Jwt.Utility.UnixTime + (60 * 5)).ToString());
claimbuilder.Add("iat", Stub.Jwt.Utility.UnixTime.ToString());
string head = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
var jwt = String.Format("{0}.{1}", head, claimbuilder.ClaimSet);
Console.WriteLine(jwt);
var certificate = new X509Certificate2(@"....-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable);
var token = new Stub.Jwt.JsonWebToken();
var jwtresult = token.Generate(head, claimbuilder.ClaimSet, certificate);
Console.WriteLine("jwt: {0}", jwtresult);
OAuth.Response resp = new OAuth.Response();
OAuth.Request auth = new OAuth.Request("https://accounts.google.com/o/oauth2/token");
auth.AddPostVar("grant_type", HttpUtility.UrlEncode("urn:ietf:params:oauth:grant-type:jwt-bearer")); // "authorization_code");
auth.AddPostVar("assertion", jwt);
auth.Go(resp);
Console.WriteLine(resp.OAuthTokenValue);