问题描述
我正在尝试通过.net客户端使用google calendar v3 api.我正在采用一种混合方法.我仅使用http post请求使用oauth2授权了我的应用程序,并且获得了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);
这是获取经过身份验证的calendarservice实例的代码:
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.
NB-我知道如何创建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 API 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对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!