public void LoadFromFile(String cacheId) { try { System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(this.GetType()); String tokenFile = Path.GetFullPath(asm.Location) + "-" + cacheId + ".gToken"; if (!File.Exists(tokenFile)) { return; } String jData = File.ReadAllText(tokenFile, Encoding.UTF8); GoogleAccessToken item = JSON.Deserialize <GoogleAccessToken>(jData); this.access_token = item.access_token; this.token_type = item.token_type; this.create_time = item.create_time; this.error = item.error; this.expires_in = item.expires_in; this.customer_id = item.customer_id; } catch { return; } }
public static GoogleAccessToken GetAccessToken(String base64CertData, String clientIdEMail, String scope, String adminDelegatedEmail, JSON.DebugMessage dbg) { try { if ((base64CertData == null) || (base64CertData == "")) { throw new Exception("Certificate data is empty"); } // certificate var certificate = new X509Certificate2(Convert.FromBase64String(base64CertData), "notasecret"); // header var header = new { typ = "JWT", alg = "RS256" }; // claimset var times = GetExpiryAndIssueDate(dbg); var claimset = new { iss = clientIdEMail, prn = adminDelegatedEmail, //Ver comentário abaixo scope = scope, aud = "https://accounts.google.com/o/oauth2/token", iat = times[0], exp = times[1], }; if (dbg != null) { try { dbg("iat", times[0].ToString() + " ==> " + new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(times[0]).ToString("yyyy-MM-dd HH:mm:ss")); } catch { } } ; if (dbg != null) { try { dbg("exp", times[1].ToString() + " ==> " + new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(times[1]).ToString("yyyy-MM-dd HH:mm:ss")); } catch { } } ; /* The email address of the user for which the application is requesting delegated access. * Sem colocar este parâmetro o Token é gerado, porém ao listar os usuário apresenta o erro: * (403) - Not Authorized to access this resource/api */ JavaScriptSerializer ser = new JavaScriptSerializer(); // encoded header var headerSerialized = ser.Serialize(header); var headerBytes = Encoding.UTF8.GetBytes(headerSerialized); var headerEncoded = Convert.ToBase64String(headerBytes); // encoded claimset var claimsetSerialized = ser.Serialize(claimset); var claimsetBytes = Encoding.UTF8.GetBytes(claimsetSerialized); var claimsetEncoded = Convert.ToBase64String(claimsetBytes); // input var input = headerEncoded + "." + claimsetEncoded; var inputBytes = Encoding.UTF8.GetBytes(input); // signiture var rsa = certificate.PrivateKey as RSACryptoServiceProvider; var cspParam = new CspParameters { KeyContainerName = rsa.CspKeyContainerInfo.KeyContainerName, KeyNumber = rsa.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2 }; var aescsp = new RSACryptoServiceProvider(cspParam) { PersistKeyInCsp = false }; var signatureBytes = aescsp.SignData(inputBytes, "SHA256"); var signatureEncoded = Convert.ToBase64String(signatureBytes); // jwt var jwt = headerEncoded + "." + claimsetEncoded + "." + signatureEncoded; var client = new WebClient(); client.Encoding = Encoding.UTF8; var uri = "https://accounts.google.com/o/oauth2/token"; var content = new NameValueCollection(); content["assertion"] = jwt; content["grant_type"] = "urn:ietf:params:oauth:grant-type:jwt-bearer"; string jData = ""; if (dbg != null) { try { dbg("JWT", jwt); } catch { } } ; try { jData = Encoding.UTF8.GetString(client.UploadValues(uri, "POST", content)); if (dbg != null) { try{ dbg("Return", jData); } catch {} } ; } catch (Exception ex) { if (dbg != null) { try { dbg("Error: " + ex.Message, ""); } catch { } } ; try { if (ex is WebException) { using (WebResponse response = ((WebException)ex).Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data)) { jData = reader.ReadToEnd(); } } } } catch { GoogleAccessToken err = new GoogleAccessToken(); err.error = ex.Message; jData = JSON.Serialize <GoogleAccessToken>(err); } } if (dbg != null) { try { dbg("Return", jData); } catch { } } ; return(JSON.Deserialize <GoogleAccessToken>(jData)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Erro on GetAccessToken: " + ex.Message); throw ex; } }