Oauth 2.0 如何从授权访问令牌创建GoogleCredential?
我有一个像这样的OAuth2令牌Oauth 2.0 如何从授权访问令牌创建GoogleCredential?,oauth-2.0,google-drive-api,drive,Oauth 2.0,Google Drive Api,Drive,我有一个像这样的OAuth2令牌 {{ "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "expires_in": "3600", "refresh_token": "xxxxxxxxxxxxxxxxxx", "token_type": "Bearer", }} 我正在尝试创建一个DriveService对象 Service = new DriveService(new BaseClientServic
{{
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"expires_in": "3600",
"refresh_token": "xxxxxxxxxxxxxxxxxx",
"token_type": "Bearer",
}}
我正在尝试创建一个DriveService对象
Service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "foo",
});
(像这样?)
但我显然做得不好,而且找不到文档
当我试图创建一个GoogleCredential
以传递给DriveService
GoogleCredential credential = GoogleCredential.FromJson(credentialAsSerializedJson).CreateScoped(GoogleDriveScope);
我得到以下例外情况:
{System.InvalidOperationException:从JSON创建凭据时出错。无法识别的凭据类型。
我是不是完全走错了路
(这是)我已经设法解决了这个问题 解决方案是使用我的ClientID和ClientSecret创建一个
Google.api.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow
Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow googleAuthFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer()
{
ClientSecrets = new ClientSecrets()
{
ClientId = ClientID,
ClientSecret = ClientSecret,
}
});
还有一个Google.api.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"],
};
并使用每个来创建一个用户凭证
,该凭证反过来用于初始化DriveService
var credential = new UserCredential(googleAuthFlow, "", responseToken);
Service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "com.companyname.testxamauthgoogledrive",
});
我更新了以反映这些更改。我不熟悉C#,但我认为有一个指南将帮助您使用授权访问令牌创建driveService。这还将提供区分用户凭据和ServiceAccountCredential的指南。希望这有帮助。我正试图做相反的事情。我有一个
GoogleCredential
我需要获取一个访问令牌。有什么想法吗?您在控制台中创建了哪种类型的应用程序?是web还是其他?因为Android或Ios没有客户端密码。您是否使用xamairn auth获取令牌?