1

I want to oauth authentication like Login using Google OAuth 2.0 with C# But i don't want to authentication prompt popup i want to get token directly without popup..

public  ActionResult CodeLele()
    {
        if (Session.Contents.Count > 0)
        {
            if (Session["loginWith"] != null)
            {
                if (Session["loginWith"].ToString() == "google")
                {
                    try
                    {
                        var url = Request.Url.Query;
                        if (url != "")
                        {
                            string queryString = url.ToString();
                            char[] delimiterChars = { '=' };
                            string[] words = queryString.Split(delimiterChars);
                            string code = words[1];

                            if (code != null)
                            {
                                //get the access token 
                                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
                                webRequest.Method = "POST";
                                Parameters = "code=" + code + "&client_id=" + googleplus_client_id + "&client_secret=" + googleplus_client_sceret + "&redirect_uri=" + googleplus_redirect_url + "&grant_type=authorization_code";
                                byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
                                webRequest.ContentType = "application/x-www-form-urlencoded";
                                webRequest.ContentLength = byteArray.Length;
                                Stream postStream = webRequest.GetRequestStream();
                                // Add the post data to the web request
                                postStream.Write(byteArray, 0, byteArray.Length);
                                postStream.Close();

                                WebResponse response = webRequest.GetResponse();
                                postStream = response.GetResponseStream();
                                StreamReader reader = new StreamReader(postStream);
                                string responseFromServer = reader.ReadToEnd();

                                GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);

                                if (serStatus != null)
                                {
                                    string accessToken = string.Empty;
                                    accessToken = serStatus.access_token;

                                    if (!string.IsNullOrEmpty(accessToken))
                                    {
                                        // This is where you want to add the code if login is successful.
                                        // getgoogleplususerdataSer(accessToken);
                                    }
                                    else
                                    { }
                                }
                                else
                                { }
                            }
                            else
                            { }
                        }
                    }
                    catch (WebException ex)
                    {
                        try
                        {
                            var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

                            dynamic obj = JsonConvert.DeserializeObject(resp);
                            //var messageFromServer = obj.error.message;
                            //return messageFromServer;
                            return obj.error_description;
                        }
                        catch (Exception exc)
                        {
                            throw exc;
                        }
                    }
                }
            }
        }
        return Content("done");
    }
    public ActionResult JeClick()


    {
        var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + googleplus_redirect_url + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + googleplus_client_id;
        Session["loginWith"] = "google";
        return Redirect(Googleurl);
    }
Community
  • 1
  • 1
Nayeem Mansoori
  • 821
  • 1
  • 15
  • 41

1 Answers1

0

The credentials window (popup) is how you ask the user if you can access their data. There is no way to get access to a users data without asking the user first if you may access their data. That is how Oauth2 works.

If you are accessing your own data then you can use something called a Service account. Service accounts are pre authorized. You can take the service account and grant it access to your google calendar, you could give it access to a folder in Google drive. Then you can authenticate using the service account. Service accounts are like dummy users.

My article about service accounts: Google Developer service account

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449