2016-05-09 5 views
1

私はこの問題についていくつかの話題を見ましたが、多分私は何かを見落としています。Googleコンタクトを認証するv3(私のGoogleカレンダートークンは使用できません)?

これは私のカレンダーのauthticateする私の現在のVBコードです:私は今も、Googleの連絡先データを操作したい

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 

。だから私は、この範囲を含めるために、上記の方法を変更:

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権限を。

私は間違っていますか?ありがとうございました。

UPDATE

私は、このWebページを見つけました:http://www.daimto.com/google-contacts-with-c/

だから私はそれのいくつかを実装してみました:ブールのよう

プライベートファンクションDoAuthentication(文字列、OAuth2ParametersとしてのByRef rParametersとしてByRefのrStrTokenを) Dim Credential As UserCredential Dim Secrets =新しいClientSecrets() Secrets.ClientId = m_strClientID Secrets.ClientSecrら= m_strClientSecret m_Scopes.Add(CalendarService.Scope.Calendar) m_Scopes.Add( "https://www.google.com/m8/feeds/")

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例外を取得します。 Google Consoleに連絡先APIを正しく追加しました。混乱している。

私は頭痛があるのでしょうか?私はこの401未分類の問題を乗り越えることはできません。

+0

Googleアカウントへの現在の接続を取り消し、再度認証して両方のサービスを行う必要があるようです。 –

答えて

0

私はそれを動作させました!私がしなければならないことがいくつかありました。

ステップ1

私が、間にスペースを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ラインをコメントアウトし、リテラルテキストを使用しました。それが最善のことであるかどうかはわかりません。

IはOAuth2Parametersオブジェクトに渡され、エントリのカップルを移入上述の方法ステップ2

ステップ3

呼び出し元のコードは、このようなものです:私はすでにPCにアクセストークンを持っていたので

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

、私はに持っていましたを無効にして、カレンダーサービスだけでなく連絡先にもアクセスできるようにします。

関連する問題