将Oauth2与Google Calendar API和vb.Net一起使用的例外情况

时间:2014-11-26 15:07:10

标签: vb.net google-api-client google-api-dotnet-client

我无法通过oauth2连接到Google日历api。我已下载* .json文件,其中包含我的cliend-id和我的cliend secret,并使用谷歌的代码示例

'Copyright 2013 Google Inc

'Licensed under the Apache License, Version 2.0(the "License");
'you may not use this file except in compliance with the License.
'You may obtain a copy of the License at

'    http://www.apache.org/licenses/LICENSE-2.0

'Unless required by applicable law or agreed to in writing, software
'distributed under the License is distributed on an "AS IS" BASIS,
'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'See the License for the specific language governing permissions and
'limitations under the License.

Imports System.Collections.Generic
Imports System.IO
Imports System.Threading

Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Calendar.v3.EventsResource
Imports Google.Apis.Services
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Util.Store

''' <summary>
''' An sample for the Calendar API which displays a list of calendars and events in the first calendar.
''' https://developers.google.com/google-apps/calendar/
''' </summary>
Module Program

    '' Calendar scopes which is initialized on the main method.
    Dim scopes As IList(Of String) = New List(Of String)()

    '' Calendar service.
    Dim service As CalendarService

    Sub Main()
        ' Add the calendar specific scope to the scopes list.
        Dim scopes As IList(Of String) = New List(Of String)()
        scopes.Add(CalendarService.Scope.Calendar)

        ' Display the header and initialize the sample.
        Console.WriteLine("Google.Apis.Calendar.v3 Sample")
        Console.WriteLine("==============================")

        dim credential As UserCredential

        Using stream As New FileStream("c:/temp/client_secrets.json", FileMode.Open, FileAccess.Read)
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets, scopes, "user", CancellationToken.None,
                    New FileDataStore("Calendar.VB.Sample")).Result
        End Using

        ' Create the calendar service using an initializer instance
        Dim initializer As New BaseClientService.Initializer()
        initializer.HttpClientInitializer = credential
        initializer.ApplicationName = "VB.NET Calendar Sample"
        service = New CalendarService(initializer)

        ' Fetch the list of calendar list
        Dim list As IList(Of CalendarListEntry) = service.CalendarList.List().Execute().Items()

        ' Display all calendars
        DisplayList(list)
        For Each calendar As Data.CalendarListEntry In list
            ' Display calendar's events
            DisplayFirstCalendarEvents(calendar)
        Next

        Console.WriteLine("Press any key to continue...")
        Console.ReadKey()
    End Sub
end class

我在GoogleWebAuthorizationBroker.AuthorizeAsync(...中)获得了一个mscorelib异常 如果&#34; stream&#34;变量获取id和秘密,但除了身份验证之外一切都很好。

有人知道我做错了什么或应该尝试什么? 哪个&#34;用户&#34;我必须插入? (我使用******@developer.gserviceaccount.com)

我也试过这个版本的代码来访问youtube api

 Private Async Function GetGredentials() As Task
        Try
            AddToLog("Begin: GetCredentials")
            '
            ' ClientId and ClientSecret are found in your client_secret_*****.apps.googleusercontent.com.json file downloaded from 
            ' the Google Developers Console ( https://console.developers.google.com). 
            ' This sample shows the ClientID and ClientSecret in the source code. 
            ' Other samples in the sample library show how to read the Client Secrets from the client_secret_*****.apps.googleusercontent.com.json file. 
            '

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

            OAUth2Credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync( _
                New ClientSecrets With {.ClientId = "242596777308-jmpe9cq9lvr23kmgt7bgsac6ogf620k8.apps.googleusercontent.com", _
                                        .ClientSecret = "beymodgzMv9kenB8ds2R5Bb_"}, _
                                    scopes, "me", CancellationToken.None)
            AddToLog("End: GetCredentials")
            If OAUth2Credential IsNot Nothing Then
                If OAUth2Credential.Token IsNot Nothing Then
                    AddToLog(String.Concat("Token Issued: ", OAUth2Credential.Token.Issued))
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Google Authorization")
            End
        End Try
    End Function

使用的ID和密码直接从谷歌的api控制台复制。代码现在抛出异常&#34;找不到应用程序&#34;

异常堆栈跟踪是:

bei Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)    bei Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务)    bei Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)    bei Microsoft.Runtime.CompilerServices.TaskAwaiter`1.GetResult()    bei YouTube_API_V3_Demo.Main.VB $ StateMachine_1_GetGredentials.MoveNext()位于C:\ Users \ Michael \ Downloads \ Sample \ YouTube_API_V3_Demo \ YouTube_API_V3_Demo \ Main.vb:Zeile 202。

我认为oauth2身份验证有问题,但是什么? 我需要在&#34;用户&#34;中插入什么?参数β

迈克尔

0 个答案:

没有答案