在Google中使用刷新令牌

发布于 2025-01-26 08:19:19 字数 1465 浏览 4 评论 0原文

我正在重写一个使用Google Contacts API(RIP)使用人员API的应用程序。我已经有一个刷新令牌。以前,我创建了一个oauth2parameters对象的实例,并用它来创建requestsetsettings类的实例,该类别将传递给contacts> 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);

我无法弄清楚如何在新的人API中做同样的事情。显然,我需要使用peopleserviceservice对象,但是其构造函数采用initializer对象的实例,我不知道如何使用刷新令牌和(可能)访问令牌。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2025-02-02 08:19:19

这是有关所有Google API的.NET库进行身份验证的官方教程:

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

这是一个有用的片段未来的身份验证尝试:

            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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文