如何使用 access_token 创建 CalendarService 对象? [英] How to make CalendarService object using access_token?

查看:22
本文介绍了如何使用 access_token 创建 CalendarService 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .net 客户端使用谷歌日历 v3 api.我正在遵循混合方法.我已使用 oauth2 仅使用 http post 请求授权我的应用程序,并获得了 access_token.但是作为calendar v3 api的.net客户端,我需要做一个calendarservice引用.我需要找到任何方法来使用我的令牌获取该服务引用.看看这个代码片段:

I am trying to use google calendar v3 api using .net client. I am follwing a hybrid approach. I have authorized my application using oauth2 using only http post request and I get the access_token. But as .net client of calendar v3 api, I need to make a calendarservice reference. I need to find any way to get that service reference using my token. Have a look at this code snippet:

Event event = new Event()
{
  Summary = "Appointment",     
};

Event recurringEvent = service.Events.Insert(event, "primary").Fetch();
// here "service" is authenticate calendarservice instance.

Console.WriteLine(recurringEvent.Id);

这是获取经过身份验证的日历服务实例的代码:

and this is the code to get authenticated calendarservice instance:

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

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

此代码显示了根据 Google.Apis.Auth.OAuth2 的授权代码流程,然后使用该凭据进行服务引用.实际上,这是一个管理授权代码流的辅助实用程序.需要明确的是,我没有使用这个程序.(这个帮助实用程序).我正在尝试在核心级别执行所有操作,这意味着我通过简单的 HTTP Web 请求手动创建了授权代码流.而且我已经完美地完成了授权.现在我有那个用户 access_token.

This code shows the authorization code flow according to Google.Apis.Auth.OAuth2 and then make service reference using that credential. Actually this is a helper utility to manage authorization code flow. To be clear, I am not using this procedure.(this helper utility). I am trying to do everything in core level that means I have made authorization code flow manually by simple HTTP web request. And I have done authorization perfectly. Now I have that users access_token.

现在我的问题是如何仅使用该 access_token 手动创建此服务实例.如果有什么困扰您,请随时提出任何问题.

Now my question is that how can I create this service instance manually only using that access_token. If anything bother you, feel free to ask anything.

注意 - 我知道如何创建 CalendarService 实例:

N.B - I know how to create CalendarService instance:

 var service = new CalendarService();

但是我怎样才能创建这个类型实例并连接到我拥有的经过身份验证的令牌.

but how can I create this type instance with connected to authenticated token which I have.

推荐答案

这个问题大约在一年前被问到,但无论如何这里是我用来初始化只有 accessToken 的 CalendarService 的代码.

The question was asked about a year ago but anyway here is the code I use to initialize CalendarService having accessToken only.

起初,我基于其源代码实现了 UserCredential 类的克隆",但删除了与 Google APIs OAuth2 方法相关的所有不必要的人员

At first, I implemented a "clone" of UserCredential class based on its source code but removing all unnecessary staff related to Google APIs OAuth2 methods

internal class CustomUserCredential : IHttpExecuteInterceptor, IConfigurableHttpClientInitializer
{
    private string _accessToken;

    public CustomUserCredential(string accessToken)
    {
        _accessToken = accessToken;
    }

    public void Initialize(ConfigurableHttpClient httpClient)
    {
        httpClient.MessageHandler.ExecuteInterceptors.Add(this);
    }

    public async Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
    }
}

之后,创建 CalendarService 的实例看起来非常简单:

After that, creating an instance of CalendarService looks pretty simple:

private CalendarService GetCalendarService(string accessToken)
    {
        return new CalendarService(new BaseClientService.Initializer
            {
                HttpClientInitializer = new CustomUserCredential(accessToken),
                ApplicationName = "AppName"
            });
    }

这篇关于如何使用 access_token 创建 CalendarService 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆