[大约6个月前,我在Google Developers Console中设置了一个Web应用程序,以便我们内部网站的员工可以启动电子邮件,该电子邮件将读取google帐户中的模板文档,合并某些字段,然后下载pdf版本发送电子邮件。
现在,我们必须将这些模板文档移至其他google管理的域/用户帐户,因此,我已在新帐户中复制了文档的副本,并使用新的文档ID更新了我们的引用。
此外,从今年第一天起,我最初在google dev控制台中创建此应用程序时收到的电子邮件就已经不见了。因此,我还必须在一个新帐户下重新创建该应用程序。
我已经做到了,并匹配了原始应用程序的所有设置。但是,当我尝试访问文档时,出现错误Google.Apis.Auth.OAuth2.Responses.TokenResponseException:'错误:“ unauthorized_client”,说明:“未授权”,Uri:“”“
我遵循this page设置了原始用户身份验证。我知道在实际使用之前要经过大量的反复试验,而且我一定会忘记一些东西。我想知道它是否与需要重新认证新应用有关。尽管我从新应用程序中指定了新的clientid和clientsecret,但没有弹出窗口要求我授予该应用程序的权限。我希望新的凭证信息会打开该窗口,请我给予许可。这是供参考的文件。有什么想法吗?
public class AuthCallbackController : Google.Apis.Auth.OAuth2.Mvc.Controllers.AuthCallbackController
{
protected override FlowMetadata FlowData => new AppFlowMetadata();
}
public class AppFlowMetadata : FlowMetadata
{
private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = AwsSecrets.GoogleCreds.ClientId,
ClientSecret = AwsSecrets.GoogleCreds.ClientSecret
},
Scopes = new[] {DriveService.Scope.Drive},
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
public override IAuthorizationCodeFlow Flow => flow;
public override string GetUserId(Controller controller)
{
return "userid";
}
}
public class GoogleController : TECWareControllerBase
{
private readonly IGoogleCredentialService _gservice;
public GoogleController(IGoogleCredentialService gservice)
{
_gservice = gservice;
}
public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken)
{
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);
if (result.Credential != null)
{
_gservice.SaveRefreshToken(result.Credential.Token.RefreshToken);
return View();
}
return new RedirectResult(result.RedirectUri);
}
}
我终于找到了一种使它工作的方法。
首先使用此方法
private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = AwsSecrets.GoogleCreds.ClientId,
ClientSecret = AwsSecrets.GoogleCreds.ClientSecret
},
Scopes = new[] {DriveService.Scope.Drive},
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
我不得不将FileDataStore(“ Drive.Api.Auth.Store”)键更改为其他类似FileDataStore(“ GoogleAuth”)]的键>
这迫使身份验证启动。
[不幸的是,谷歌随后抱怨无效的重定向uri。以下代码返回了http://localhost:11224/AuthCallback/IndexAsync的重定向uri,该重定向uri在我的Web应用程序的授权重定向uri中甚至都不存在。应该是http://localhost:11224/MVC/AuthCallback/IndexAsync。因此,在url结果的重定向url中,我将其更改为应有的内容,这使我可以完成授权。现在,我可以访问经过身份验证的帐户中的文档。
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);