尝试使用OAuth2会导致NOT授权客户端或范围错误

时间:2014-10-22 21:50:29

标签: vb.net authorization

我需要获得授权才能通过服务帐户使用Calendar API。

当我发出-Dim ok As Boolean = Await GetToken-指令时,我收到错误,我无法继续。请有人帮帮我吗? TIA

+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++ 错误详情:   Google.Apis.Auth.OAuth2.Responses.TokenResponseExceptionnonèstatagestita   的HResult = -2146233088   消息=错误:“unauthorized_client”,描述:“请求中未经授权的客户端或范围。”,Uri:“”   来源= Microsoft.Threading.Tasks   堆栈跟踪:        在Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)        在Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务)        在Google.Apis.Auth.OAuth2.ServiceAccountCredential.d__b.MoveNext()中的c:\ code \ google.com \ google-api-dotnet-client \ default \ Tools \ Google.Apis.Release \ bin \ Debug \ test \ default \ Src \ GoogleApis.Auth.DotNet4 \ OAuth2 \ ServiceAccountCredential.cs:riga 282     ---精美的traccia dello stack da posizione前身doveèstatagenerata l'eccezione ---        在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中        在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中        在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中        在D:\ inetpub \ wwwroot \ UGO_Cont.Web \ Services \ GoogleCalendarMgt.svc.vb中的UGO_Cont.GoogleCalendarMgt.VB $ StateMachine_0_GetAccessToken.MoveNext()中:riga 160     ---精美的traccia dello stack da posizione前身doveèstatagenerata l'eccezione ---        在System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__5(对象状态)        在System.Threading.ExecutionContext.RunInternal中(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx)        在System.Threading.ExecutionContext.Run中(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx)        在System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()中        在System.Threading.ThreadPoolWorkQueue.Dispatch()中   的InnerException: ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++

这是我使用的代码:

Private Const SVCACCMAIL = "999999999999-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com"
Private Const ACTIVITY_ID = "x9x9x9x9x9x9x9x9x9x9x9x9x9x9x9x9x9x9x9"

<OperationContract()>
Public Function InsertEvent(authToken As String, id_Azienda As Integer, id_Azione As Integer, id_Op As Integer) As String
    Dim scopes As New List(Of String)
    scopes.Add("https://www.googleapis.com/auth/calendar")
    Dim inzlzr As New ServiceAccountCredential.Initializer(SVCACCMAIL) With {.User = SVCACCMAIL, .Scopes = scopes}
    Dim credential As New ServiceAccountCredential(inzlzr.FromCertificate(GetCert))

    GetAccessToken(credential)
End Function

Public Function GetCert() As X509Certificate2
    Dim fileCert As String = "D:\inetpub\wwwroot\UGO_Cont.Web\UGO-Adv-CRM-999999999999999a.p12"
    Dim cert = New X509Certificate2(fileCert, "notasecret", X509KeyStorageFlags.Exportable)
    Return cert
End Function

Async Sub GetAccessToken(cr As ServiceAccountCredential)
    Dim GetToken = cr.RequestAccessTokenAsync(System.Threading.CancellationToken.None)

    Dim ok As Boolean = Await GetToken
    Dim service As New CalendarService(New BaseClientService.Initializer() With {.ApplicationName = "Test calendar sync app", .HttpClientInitializer = cr})

    Dim list As IList(Of CalendarListEntry) = service.CalendarList.List().Execute().Items()
End Sub

1 个答案:

答案 0 :(得分:1)

观察一些工作样本,我发现ServiceAccounCtredential的初始化程序没有&#34;。用户&#34;参数。因此,删除此参数,我没有更多的身份验证错误。所以我认为身份验证错误已经消失。这些是工作线:

    Dim inzlzr As New ServiceAccountCredential.Initializer(SVCACCMAIL) With {.Scopes = scopes}

非常感谢每个看过这个问题的人。

(现在我开始尝试制作工作日历&#39;事件插入以同步我们的内部CRM)

S.Tedeschi