使用Access和Refresh Tokens .net访问Google API OAuth2

时间:2014-03-19 21:08:16

标签: vb.net api

我正在尝试弄清楚如何使用OAuth2永久访问用户Google日历。我现在可以让用户授权访问,我有来自Google的AccessToken,RefreshToken和/或完整的响应令牌存储在数据库中。

我面临的问题是如何将这些令牌添加到Google API中的凭据中。我发现没有可行的例子。我尝试了一些事情。它可能很简单,因为我错过了一个我无法弄清楚的dll的引用。 GoogleCredential.Builder()似乎是要走的路,但除了Java API之外我无法找到它。

这是我得到的最接近的。问题是如何创建正确的凭据。

  Dim user As CRMUser = GetUser(Membership.GetUser().ProviderUserKey)

        Dim scopes As IList(Of String) = New List(Of String)()
        scopes.Add(CalendarService.Scope.Calendar)


        Dim ClientID As String = System.Configuration.ConfigurationManager.AppSettings("GoogleCalendarApplicationClientID").ToString()
        Dim ClientSecret As String = System.Configuration.ConfigurationManager.AppSettings("GoogleCalendarApplicationClientSecret").ToString()
        Dim service As CalendarService

        Dim credential As GoogleCredential = New GoogleCredential.Builder().setJsonFactory(jsonFactory).setTransport(httpTransport).setClientSecrets(ClientID, ClientSecret).build()
        credential.setAccessToken(user.GoogleUserToken)
        credential.setRefreshToken(user.GoogleRefreshToken)


        Dim initializer As New BaseClientService.Initializer()
        initializer.HttpClientInitializer = credential
        initializer.ApplicationName = "VB.NET Calendar Sample"
        service = New CalendarService(initializer)

0 个答案:

没有答案