【发布时间】:2016-11-23 12:59:55
【问题描述】:
我正在尝试使用 .NET API 从 Google Drive 下载文件。
private UserCredential _credential;
protected void Authorize()
{
var scopes = new[] {DriveService.Scope.DriveReadonly};
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
var credentialPath = @"App_Data\credential";
_credential =
new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
Scopes = scopes
}), Environment.UserName, new TokenResponse());
// I tried both ways, both result the same exception
//_credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, scopes, Environment.UserName, CancellationToken.None).Result;
}
}
public Download SaveAsDownload(string fileId)
{
if(_credential == null)
Authorize();
var service = new DriveService(new BaseClientService.Initializer()
{
ApiKey = ApiKey,
HttpClientInitializer = _credential
});
var getRequest = service.Files.Get(fileId);
var file = getRequest.Execute(); // When I call this I get the exception
return null;
}
每次我尝试这样做时,我都会收到“访问令牌已过期但我们无法刷新它”异常。我找到的唯一答案是尝试其他身份验证方式,但我都尝试了,但都导致了相同的异常。 我尝试了 v2 和 v3 版本的 Google SDK。
【问题讨论】:
标签: c# google-api google-oauth google-api-dotnet-client google-authentication