/// <summary> /// Refresh access token. /// </summary> /// <returns></returns> public async Task <Token> RefreshToken() { if (this.Token != null) { if (!string.IsNullOrWhiteSpace(Token.RefreshToken)) { var requestUrl = RequestUri.GetRequestUrl(Base.GoogleApis, Api.Outh2, ApiVersion.V4, Endpoint.RefreshToken); #region Set Parameters var parameters = new Dictionary <string, string>() { { "client_id", this.Credentials.ClientId }, { "client_secret", this.Credentials.ClientSecret }, { "refresh_token", this.Token.RefreshToken }, { "grant_type", GrantType.RefreshToken.GetDescription() }, }; #endregion requestUrl = requestUrl.Append(parameters); var response = await HttpHelper.Instance.PostAsync(requestUrl, ""); var tokenObject = !string.IsNullOrWhiteSpace(response) ? JsonConvert.DeserializeObject <Token>(response) : null; if (tokenObject != null) { this.Token = tokenObject; AnalyticsExtensions.Init(this.Credentials, this.Token); } return(tokenObject); } else { throw new Exception("Refresh token not found!"); } } else { throw new Exception("Token not found!"); } }
/// <summary> /// Get auth acccess token from google. (For enable scopes) /// </summary> /// <param name="code"></param> /// <returns></returns> public async Task <Token> Auth(string code) { if (string.IsNullOrWhiteSpace(code)) { throw new Exception("Code is not valid!"); } var requestUrl = RequestUri.GetRequestUrl(Base.GoogleApis, Api.Auth, ApiVersion.V4, Endpoint.Token); #region Set Parameters var parameters = new Dictionary <string, string>() { { "code", code }, { "client_id", this.Credentials.ClientId }, { "client_secret", this.Credentials.ClientSecret }, { "redirect_uri", this.Credentials.RedirectUrl }, { "grant_type", GrantType.Code.GetDescription() } }; #endregion requestUrl = requestUrl.Append(parameters); var response = await HttpHelper.Instance.PostAsync(requestUrl, ""); var tokenObject = !string.IsNullOrWhiteSpace(response) ? JsonConvert.DeserializeObject <Token>(response) : null; if (tokenObject != null) { this.Token = tokenObject; AnalyticsExtensions.Init(this.Credentials, this.Token); } return(tokenObject); }