public IActionResult Google() { var gmailAuth = new GmailAuth(); gmailAuth.client_id = "client_id"; gmailAuth.client_secret = "client_secret"; gmailAuth.auth_uri = "https://accounts.google.com/o/oauth2/auth"; gmailAuth.token_uri = "https://accounts.google.com/o/oauth2/token"; var domainName = "http://localhost:5000"; gmailAuth.response_type = "code"; gmailAuth.scope = "https://www.googleapis.com/auth/userinfo.email"; gmailAuth.redirect_uri = $"{domainName}/home/getgoogledata/sign-in"; var urlLink = new GmailAuthHelper().GetUrlForAuthorizationCode(gmailAuth); // var urlLink = $"{gmailAuth.auth_uri}?client_id={gmailAuth.client_id}&response_type={}&redirect_uri={gmailAuth.redirect_uri}&scope={gmailAuth.scope}"; return(Redirect(urlLink)); }
public async Task <GoogleUserInfo> GetGoogleData(string code) { var gmailAuth = new GmailAuth(); gmailAuth.client_id = "client_id"; gmailAuth.client_secret = "client_secret"; gmailAuth.auth_uri = "https://accounts.google.com/o/oauth2/auth"; gmailAuth.token_uri = "https://accounts.google.com/o/oauth2/token"; var domainName = "http://localhost:5000"; // var responseType="code"; // var scope="https://www.googleapis.com/auth/userinfo.profile"; gmailAuth.redirect_uri = $"{domainName}/home/getgoogledata/sign-in"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response; var objJson = new GmailAuthHelper().GetUrlEncodedContent(gmailAuth, code); response = await client.PostAsync(gmailAuth.token_uri, objJson); if (response.IsSuccessStatusCode) { var jsondata = response.Content.ReadAsStringAsync().Result; GoogleData mytoken = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject <GoogleData>(jsondata)); GoogleUserInfo userinfo = await GetUserDetails(mytoken.Access_token); return(userinfo); } else { return(null); } } }