'Use refresh token in Google's People API Client Library for .NET

I am rewriting an app that used Google Contacts API (RIP) to use People API. I already have a refresh token. Previously, I created an instance of the OAuth2Parameters object, and used it to create an instance of the RequestSettings class to be passed to the ContactsRequest constructor

            OAuth2Parameters oparams = new OAuth2Parameters
            {
                AccessToken = tokenData.access_token,
                RefreshToken = tokenData.refresh_token,
                ClientId = ClientId,
                ClientSecret = ClientSecret,
                AccessType = "offline",
                ApprovalPrompt = "force",
                Scope = _contactScope
            };

            if (string.IsNullOrWhiteSpace(oparams.AccessToken))
            {
                oparams.AccessToken = "xyz";  //it doesn't matter what this token is, it just can't be blank, it will get refreshed
                OAuthUtil.RefreshAccessToken(oparams);
                dataStore._storedResponse.access_token = oparams.AccessToken;
            }
        
            var settings = new RequestSettings("My App")
            {
                OAuth2Parameters = oparams
            };

            if (paging)
            {
                settings.PageSize = 50;
                settings.AutoPaging = true;
            }
            return new ContactsRequest(settings);

I cannot figure out how to do the same in the new world of People API. I obviously need to use PeopleServiceService object, but its constructor takes an instance of the Initializer object, and I don't know out how I can initialize it with the refresh token and (possibly) access token.



Solution 1:[1]

Here's the official tutorial on how to do authentication with the .NET library for all Google APIs:

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

Here's a useful snippet from it that will also help with persisting the refresh token to a file and use it in future authentication attempts:

            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { BooksService.Scope.Books },
                    "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
            }

            // Create the service.
            var service = new BooksService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Books API Sample",
                });

            var bookshelves = await service.Mylibrary.Bookshelves.List().ExecuteAsync();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marmor