2

I am trying to use OAuth 2 in my Asp.net app (C#). The problem is I need to use a shared google account. To do this, my plan is to seed the authentication with a token, an expiration date, and a refresh token, and then when authentication is required I check the expiration date and use the refresh token.

The example I've been using for Authentication looks like this:

    UserCredential credential;
    using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
    {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            // This OAuth 2.0 access scope allows an application to upload files to the
            // authenticated user's YouTube channel, but doesn't allow other types of access.
            new[] { YouTubeService.Scope.YoutubeUpload },
            "user",
            CancellationToken.None
        );
    }

And doesn't seem to contain an object with a refresh token.

How do I get the Refresh token and expiration date?

1 Answer 1

4

The solution was to do a post action and parse the results manually instead of using any of the Google classes.

        string gurl = "code=" + code + "&client_id=" + client_id +
                "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=" + grant_type;

        string url = "https://www.googleapis.com/oauth2/v3/token";

        // creates the post data for the POST request
        string postData = (gurl);

        // create the POST request
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Host ="www.googleapis.com";

        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = postData.Length;

        // POST the data
        using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
        {
            requestWriter2.Write(postData);
        }

        //This actually does the request and gets the response back
        HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();

        string googleAuth;

        using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
        {
            //dumps the HTML from the response into a string variable
            googleAuth = responseReader.ReadToEnd();
        }

From there I mainly need to parse the googleAuth string to get at the Token, the Refresh Token, and the expiration period. I kept expecting there to be a solution inside the Google classes for what must be an incredibly common request, but apparently I'll be creating my own class.

Sign up to request clarification or add additional context in comments.

1 Comment

should I store this access_token? bcz each it will generate new access_token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.