我正在开发一个 Asp.NET MVC5 应用程序按照这个谷歌示例代码 https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth.
我希望应用程序经过身份验证并由用户创建访问令牌(配置阶段),稍后我需要能够使用刷新令牌调用 Google API'(在我的情况下是目录 API)(无需用户干预,例如“离线”)。
控制器:
public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken)
{
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
AuthorizeAsync(cancellationToken);
if (result.Credential != null)
{
var service = new Google.Apis.Admin.Directory.directory_v1.DirectoryService(new BaseClientService.Initializer
{
HttpClientInitializer = result.Credential,
ApplicationName = "My Application"
});
return View();
}
else
{
return new RedirectResult(result.RedirectUri);
}
}
流元数据实现。 (我正在使用稍微修改过的 FiledataStore (GoogleFileDataStore))
public class AppFlowMetadata : FlowMetadata
{
//Move to settings
static readonly string clientId = "xxxccnvofsdfsfoj.apps.googleusercontent.com";
static readonly string clientsecret = "xxxxxxxxxxLvtC6Qbqpp4x_";
static readonly string datastore = "AdminSDK.Api.Auth.Store";
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientsecret
},
Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser, DirectoryService.Scope.AdminDirectoryUserAliasReadonly },
DataStore = new GoogleFileDataStore(datastore)
});
public override string GetUserId(Controller controller)
{
return ConfigHelper.UserID.ToString();
}
public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
当我调用 IndexAsync 控制器时,由于用户之前没有访问令牌,因此它会在用户登录 Google 帐户后创建一个新的访问令牌。
这是一个示例访问令牌,
{"access_token":"ya29.5gBOszGO-oYJJt4YZfF6FeaZth1f69_.....","token_type":"Bearer","expires_in":3600,"Issued":"2014-12-24T16:02:32.014+05:30"}
问题1:为什么这里没有存储Refreshtoken?如何检索刷新令牌?
问题2:如果获得了refreshtoken,我应该如何修改代码来创建新的access token并调用API(当accesstoken过期时)?
[我提到这个问题 https://stackoverflow.com/questions/25144667/using-the-youtube-v3-data-api-for-net-how-is-it-possible-to-get-a-refresh-toke,但对于缺少刷新令牌没有正确的答案。