2017-06-02 6 views
0

私は承認されたaccess_tokenからGoogle認定資格を作成するにはどうすればよいですか?

{{ 
    "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
    "expires_in": "3600", 
    "refresh_token": "xxxxxxxxxxxxxxxxxx", 
    "token_type": "Bearer", 
}} 

...このようなのOAuth2トークンを持っていると私は(このように?)

Service = new DriveService(new BaseClientService.Initializer() 
{ 
    HttpClientInitializer = credential, 
    ApplicationName = "foo", 
}); 

... DriveServiceオブジェクトを作成しようとしてい

けど明らかにこれを正しく行っていないし、ドキュメントを見つけるのが難しいです。

{のSystem.InvalidOperationException:JSONからの作成中にエラーが発生しまし資格

は、私は次の例外を取得しDriveService

GoogleCredential credential = GoogleCredential.FromJson(credentialAsSerializedJson).CreateScoped(GoogleDriveScope); 

に渡すGoogleCredentialを作成しようとします。認識できない認証情報タイプ。

私はこれについて完全に間違った方法ですか?

(これはthe sample code contextある)

+0

私はC#に精通していませんが、[OAuth 2.0 in C#](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web- applications-aspnet-mvc)は、許可されたaccess_tokenでdriveServiceを作成するのに役立ちます。これは、ユーザー資格情報とServiceAccountCredentialを区別するためのガイドも提供します。お役に立てれば。 –

答えて

1

私はこれを理解するために管理しています。解決策は、私のクライアントIDとClientSecretとGoogle.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow ...

Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow googleAuthFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer() 
{ 
    ClientSecrets = new ClientSecrets() 
    { 
    ClientId = ClientID, 
    ClientSecret = ClientSecret, 
    } 
}); 

ともGoogle.Apis.Auth.OAuth2.Responses.TokenResponseを作成することでした

Google.Apis.Auth.OAuth2.Responses.TokenResponse responseToken = new TokenResponse() 
{ 
    AccessToken = SavedAccount.Properties["access_token"], 
    ExpiresInSeconds = Convert.ToInt64(SavedAccount.Properties["expires_in"]), 
    RefreshToken = SavedAccount.Properties["refresh_token"], 
    Scope = GoogleDriveScope, 
    TokenType = SavedAccount.Properties["token_type"], 
}; 

と今度は、あるUserCredentialを作成するために、それぞれを使用DriveServiceを初期化するために使用されます。

var credential = new UserCredential(googleAuthFlow, "", responseToken); 

Service = new DriveService(new BaseClientService.Initializer() 
{ 
    HttpClientInitializer = credential, 
    ApplicationName = "com.companyname.testxamauthgoogledrive", 
}); 

これらの変更を反映するようにTestXamAuthGoogleDrive test harness projectを更新しました。

関連する問題