在VB Net上使用Google通讯录API时出现错误401

时间:2016-01-05 17:24:50

标签: vb.net google-contacts

我今天制作了一个vb net应用程序来获取我的联系人,它工作了一个小时然后当我尝试使用我的应用程序时出现此错误:enter image description here

这是我使用的代码:

Imports System.Threading
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports Google.Contacts
Imports Google.GData.Contacts
Imports Google.GData.Client
Imports Google.GData.Extensions
Public Class Form1
Dim initializer = New BaseClientService.Initializer

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim Secrets = New ClientSecrets()
    Secrets.ClientId = "MYCLIENTID"
    Secrets.ClientSecret = "MYCLIENTSECRET"

    Dim scopes As String() = New String() {"https://www.googleapis.com/auth/contacts.readonly"}
    Try
        Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scopes, "email@email.com", CancellationToken.None, New FileDataStore("MYAPPNAME")).Result()
        Dim parameters As New OAuth2Parameters()
        parameters.AccessToken = credential.Token.AccessToken
        parameters.RefreshToken = credential.Token.RefreshToken
        Dim settings As New RequestSettings("MYAPPNAME", parameters)
        Dim cr As New ContactsRequest(settings)
        Dim f As Feed(Of Contact) = cr.GetContacts()
        For Each c As Contact In f.Entries
            MsgBox(c.Name.FullName)
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
End Class

我该如何解决这个问题?

为什么在表现良好之后会发生这种情况?

由于

1 个答案:

答案 0 :(得分:0)

我解决了改变这一行的形式:

Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scopes,"email@email.com", CancellationToken.None, New FileDataStore("MYAPPNAME")).Result()

这一行:

Dim credential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {
            .ClientId = "MYCLIENTID",
            .ClientSecret = "MYCLIENTSECRET"
        }, scopes, "email@gmail.com", CancellationToken.None, New FileDataStore("MYAPPNAME")).Result

并取出秘密部分。

谢谢大家的建议。