【问题标题】:Trying Using OAuth2 causes a NOT authorized client or scope error尝试使用 OAuth2 会导致未授权的客户端或范围错误
【发布时间】:2014-12-18 12:14:27
【问题描述】:

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

只要我发出 -Dim ok As Boolean = Await GetToken- 指令,我就会收到一个错误,我无法继续。请问有人可以帮助我吗? TIA

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++ 错误详情: Google.Apis.Auth.OAuth2.Responses.TokenResponseException non è stata gestita H结果=-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\默认\Src\GoogleApis.Auth.DotNet4\OAuth2\ServiceAccountCredential.cs:riga 282 --- Fine traccia dello stack da posizione prente dove è stata Generata l'eccezione --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 UGO_Cont.GoogleCalendarMgt.VB$StateMachine_0_GetAccessToken.MoveNext() 在 D:\inetpub\wwwroot\UGO_Cont.Web\Services\GoogleCalendarMgt.svc.vb:riga 160 --- Fine traccia dello stack da posizione prente dove è stata Generata l'eccezione --- 在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__5(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback 回调, 对象状态, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 在 System.Threading.ThreadPoolWorkQueue.Dispatch() 内部异常: ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++

这是我使用的代码:

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

【问题讨论】:

    标签: vb.net authorization


    【解决方案1】:

    查看一些工作示例,我发现 ServiceAccounCtredential 的初始化程序没有“.User”参数。因此,去掉这个参数,我没有更多的身份验证错误。所以我认为身份验证错误已经消失。这些是工作线:

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

    感谢每一位观看此问题的人。

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

    S.泰德斯基

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 2017-02-22
      • 2017-01-19
      • 2015-02-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      相关资源
      最近更新 更多