How to make CalendarService object using access_token?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 05:36:10

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

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

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"
            });
    }

The example you uses FileDataStore.

How FileDataStore works - The first time you run the code it will ask the user if they want to let you access there calender. The information is then stored in your %appData% directory. If you want to load a refresh token that you have for example stored in the database you cant.

Stored Refreshtoken - In order to use a refreshToken that you for example have stored in the database you need to create your own implimitation of IdataStore. Once you have done that you will be able to send the refresh token that you saved previously.

This tutorial should help you understand http://www.daimto.com/google-oauth2-csharp/

You dont need to deal with getting new access token the Service will use the RefreshTokens to get a new access token for you.

If this doesnt help post a comment and i will see if i can expend it a little more.

I like @alexey's response the best but as an alternative you can initialize with the simple constructor and then pass the access token in on each request, like this:

// Create service
var service = new CalendarService();

// Request
var request = service.Events.Get("x", "y");
request.OauthToken = accessToken;
var response = request.Execute();
peleyal

Your solution should look very similar to this one: .NET Google api 1.7 beta authenticating with refresh token

Remember to set the ExpiresInSeconds and Issued properties, so the library won't think that the access_token has expired (https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.Auth/OAuth2/Responses/TokenResponse.cs#66)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!