验证Google通讯录v3(我不能使用我的Google日历令牌)吗?

时间:2016-05-09 11:28:46

标签: vb.net oauth-2.0 google-contacts

我在这个问题上看到了一些话题,但也许我忽视了一些事情。

这是我当前用于为我的日历创作的VB代码:

Private Function DoAuthentication(ByRef rStrToken As String) As Boolean
    Dim credential As UserCredential
    Dim Secrets = New ClientSecrets()
    Secrets.ClientId = m_strClientID
    Secrets.ClientSecret = m_strClientSecret
    m_Scopes.Add(CalendarService.Scope.Calendar)

    Try
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, m_Scopes,
                                                                 "user", CancellationToken.None,
                                                                 New FileDataStore("xxxxx.Calendar.Application")).Result()

        ' Create the calendar service using an initializer instance
        Dim initializer As New BaseClientService.Initializer()
        initializer.HttpClientInitializer = credential
        initializer.ApplicationName = "Public Talks And Meeting Schedule Assistant Calendar"
        m_Service = New CalendarService(initializer)

        rStrToken = credential.Token.AccessToken.ToString()
    Catch ex As Exception
        ' We encountered some kind of problem, perhaps they have not yet authenticated?
        ' Can we isolate that as the exception?
        Return False
    End Try

    Return True
End Function

我现在也想使用Google联系人数据。所以我修改了上面的方法来包含这个范围:

m_Scopes.Add("https://www.google.com/m8/feeds/")

然后我尝试使用它:

If (DoAuthentication(strToken)) Then
    Dim settings As New RequestSettings("xx", strToken)
    settings.AutoPaging = True
    iResult = RESULT_SUCCESS_OAUTH
    m_ContactsRequest = New ContactsRequest(settings)
    Dim f As Feed(Of Contact) = m_ContactsRequest.GetContacts()

    For Each oContact As Contact In f.Entries
        Console.WriteLine(oContact.Name.ToString)
    Next
Else
    Return RESULT_FAILED_OAUTH
End If

但它引发了一个例外:401 Unauthorized。

我做错了什么?谢谢。

更新

我找到了这个网页:http://www.daimto.com/google-contacts-with-c/

所以我尝试实施其中一些:

私有函数DoAuthentication(ByRef rStrToken As String,ByRef rParameters As OAuth2Parameters)As Boolean     Dim凭证As UserCredential     昏暗的秘密=新的ClientSecrets()     Secrets.ClientId = m_strClientID     Secrets.ClientSecret = m_strClientSecret     m_Scopes.Add(CalendarService.Scope.Calendar)     m_Scopes.Add(" https://www.google.com/m8/feeds/&#34)

Try
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, m_Scopes,
                                                             "user", CancellationToken.None,
                                                             New FileDataStore("xx.Calendar.Application")).Result()

    ' Create the calendar service using an initializer instance
    Dim initializer As New BaseClientService.Initializer()
    initializer.HttpClientInitializer = credential
    initializer.ApplicationName = "Public Talks And Meeting Schedule Assistant Calendar"
    m_Service = New CalendarService(initializer)

    rStrToken = credential.Token.AccessToken.ToString()
    rParameters.AccessToken = credential.Token.AccessToken
    rParameters.RefreshToken = credential.Token.RefreshToken
Catch ex As Exception
    ' We encountered some kind of problem, perhaps they have not yet authenticated?
    ' Can we isolate that as the exception?
    Return False
End Try

代码:

   Dim parameters As New OAuth2Parameters
    If (DoAuthentication(strToken, parameters)) Then
        Dim settings As New RequestSettings("GoogleCalIF", parameters)
        settings.AutoPaging = True
        iResult = RESULT_SUCCESS_OAUTH
        m_ContactsRequest = New ContactsRequest(settings)
        Dim f As Feed(Of Contact) = m_ContactsRequest.GetContacts()

        For Each oContact As Contact In f.Entries
            Console.WriteLine(oContact.Name.ToString)
        Next
    Else
        Return RESULT_FAILED_OAUTH
    End If

    Return True
End Function

但我仍然得到401例外。我已将Contacts API正确添加到我的Google控制台。困惑。

恐怕我头疼。我无法解决这个401 Unathorized问题。

1 个答案:

答案 0 :(得分:0)

我搞定了!我必须做一些事情。

第1步

似乎我需要范围是一行字符串,其间有空格。所以我将我的身份验证方法更改为:

Private Function DoAuthentication(ByRef rStrToken As String, ByRef rParameters As OAuth2Parameters) As Boolean

    Dim credential As UserCredential
    Dim Secrets = New ClientSecrets()
    Secrets.ClientId = m_strClientID
    Secrets.ClientSecret = m_strClientSecret
    m_Scopes.Add("https://www.googleapis.com/auth/calendar https://www.google.com/m8/feeds/")

    Try
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, m_Scopes,
                 "user", CancellationToken.None,
                 New FileDataStore("PublicTalkSoftware.Calendar.Application")).Result()

        ' Create the calendar service using an initializer instance
        Dim initializer As New BaseClientService.Initializer()
        initializer.HttpClientInitializer = credential
        initializer.ApplicationName = "Public Talks And Meeting Schedule Assistant Calendar"
        m_Service = New CalendarService(initializer)

        rStrToken = credential.Token.AccessToken.ToString()
        rParameters.AccessToken = credential.Token.AccessToken
        rParameters.RefreshToken = credential.Token.RefreshToken
    Catch ex As Exception
        ' We encountered some kind of problem, perhaps they have not yet authenticated?
        ' Can we isolate that as the exception?
        Return False
    End Try

    Return True
End Function

我注释掉了 CalendarService.Scope.Calendar 这一行,并使用了文字文字。不确定这是否是最好的事情。

第2步

在上述方法中,我传入了 OAuth2Parameters 对象并填充了几个条目。

第3步

调用代码是这样的:

Dim m_ContactsRequest As ContactsRequest
Dim strToken As String = ""
Dim iResult As Integer = 0
Dim parameters As New OAuth2Parameters
If (DoAuthentication(strToken, parameters)) Then
   iResult = RESULT_SUCCESS_OAUTH
Else
   Return RESULT_FAILED_OAUTH
End If

Try
   Dim settings As New RequestSettings("XX", parameters)
   m_ContactsRequest = New ContactsRequest(settings)
   Dim f As Feed(Of Contact) = m_ContactsRequest.GetContacts()

   For Each oContact As Contact In f.Entries
         Console.WriteLine(oContact.Name.FullName.ToString)
   Next
Catch ex As Exception
    ' We encountered some kind of problem, perhaps they have not yet authenticated?
    ' Can we isolate that as the exception?
    Return False
End Try

第4步

由于我已在PC上安装了访问令牌,因此我必须撤销,这样我才能访问联系人,而不仅仅是日历服务。