Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Oauth 2-如何使用刷新令牌_C#_.net_Google Api_Google Oauth_Google Api Dotnet Client - Fatal编程技术网

C# Oauth 2-如何使用刷新令牌

C# Oauth 2-如何使用刷新令牌,c#,.net,google-api,google-oauth,google-api-dotnet-client,C#,.net,Google Api,Google Oauth,Google Api Dotnet Client,我正在使用GoogleAPI对用户日历进行更改—与客户端应用程序中的日历同步。我找不到任何关于如何以及何时使用刷新令牌的教程。下面是我的测试应用程序,可以读取/写入谷歌日历。如何检查访问令牌是否过期,应在何处执行,以及如何刷新令牌 using Google.Apis.Auth.OAuth2; using Google.Apis.Calendar.v3; using Google.Apis.Calendar.v3.Data; using Google.Apis.Services; using Go

我正在使用GoogleAPI对用户日历进行更改—与客户端应用程序中的日历同步。我找不到任何关于如何以及何时使用刷新令牌的教程。下面是我的测试应用程序,可以读取/写入谷歌日历。如何检查访问令牌是否过期,应在何处执行,以及如何刷新令牌

using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CalendarQuickstart
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/calendar-dotnet-quickstart.json
        static string[] Scopes = { CalendarService.Scope.Calendar };
        static string ApplicationName = "test";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream = new FileStream("NewFolder1/client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/test.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user1234",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;

            }

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });


            // Define parameters of request.
            EventsResource.ListRequest request = service.Events.List("primary");
            request.TimeMin = DateTime.Now;
            request.ShowDeleted = false;
            request.SingleEvents = true;
            request.MaxResults = 10;
            request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

            // List events.
            Events events = request.Execute();
            Console.WriteLine("Upcoming events:");
            if (events.Items != null && events.Items.Count > 0)
            {
                foreach (var eventItem in events.Items)
                {
                    string when = eventItem.Start.DateTime.ToString();
                    if (String.IsNullOrEmpty(when))
                    {
                        when = eventItem.Start.Date;
                    }
                    Console.WriteLine("{0} ({1})", eventItem.Summary, when);
                }
            }
            else
            {
                Console.WriteLine("No upcoming events found.");
            }

            /////////////
            Event newEvent = new Event()
            {
                Summary = "Google I/O 2015",
                Description = "A chance to hear more about Google's developer products.",

                Start = new EventDateTime()
                {
                    DateTime = DateTime.Now,
                },
                End = new EventDateTime()
                {
                    DateTime = DateTime.Now.AddHours(1),
                },
            };

            String calendarId = "primary";
            EventsResource.InsertRequest requestInsertEvent = service.Events.Insert(newEvent, calendarId);
            Event createdEvent = requestInsertEvent.Execute();
            Console.WriteLine("Event created: {0}", createdEvent.Id);
            /////////////

            Console.Read();

        }
    }
}

您不需要测试访问令牌是否过期,客户端库将为您处理该问题

然而,如果出于某种原因,你真的想把它发送给

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=1/fFBGRNJru1FQd44AzqT3Zg

如果无效,它将返回一个错误

您不需要测试访问令牌是否过期,客户端库将为您处理该问题

然而,如果出于某种原因,你真的想把它发送给

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=1/fFBGRNJru1FQd44AzqT3Zg

如果无效,它将返回一个错误

DaImTo是对的,您不必担心,客户机库会帮您做到这一点

此处提供了教程和更多详细信息: 包括提及以下内容的章节:


UserCredential和AuthorizationCodeFlow负责自动“刷新”令牌,这意味着获取一个新的访问令牌。这是使用长寿命刷新令牌完成的,如果在授权代码流期间使用access_type=offline参数,您将收到该令牌以及访问令牌。

DaImTo是正确的,您不必担心,客户端库将为您做这件事

此处提供了教程和更多详细信息: 包括提及以下内容的章节:


UserCredential和AuthorizationCodeFlow负责自动“刷新”令牌,这意味着获取一个新的访问令牌。这是使用长寿命刷新令牌完成的,如果您在授权代码流期间使用access_type=offline参数,您将收到该令牌以及访问令牌。

因此我不需要进行任何刷新?不,您不需要。刷新令牌使用文件数据存储存储。客户端库有它们,它将在需要时刷新访问令牌。因此,我不需要进行任何刷新?不,您不需要。刷新令牌使用文件数据存储存储。客户端库拥有它们,它将在需要时刷新访问令牌。