我目前正致力于 Google Api ,旨在吸引已登录人员的圈子。我已经拥有访问令牌,但问题是每当我尝试运行我的代码,它返回此异常
访问令牌已过期但我们无法刷新
如何解决此问题?
var claimsforUser = await UserManager.GetClaimsAsync(User.Identity.GetUserId());
var access_token = claimsforUser.FirstOrDefault(x => x.Type == "urn:google:accesstoken").Value;
string[] scopes = new string[] {PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile};
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "xx-xx.apps.googleusercontent.com",
ClientSecret = "v-xx",
},
Scopes = scopes,
DataStore = new FileDataStore("Store"),
});
var token = new TokenResponse { AccessToken = access_token, ExpiresInSeconds=50000};
var credential = new UserCredential(flow, Environment.UserName, token);
PlusService service = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "ArcaneChatV2",
});
PeopleResource.ListRequest listPeople = service.People.List("me", PeopleResource.ListRequest.CollectionEnum.Visible);
listPeople.MaxResults = 10;
PeopleFeed peopleFeed = listPeople.Execute();
var people = new List<Person>();
while (peopleFeed.Items != null)
{
foreach (Person item in peopleFeed.Items)
{
people.Add(item);
}
if (peopleFeed.NextPageToken == null)
{
break;
}
listPeople.PageToken = peopleFeed.NextPageToken;
// Execute and process the next page request
peopleFeed = listPeople.Execute();
}
答案 0 :(得分:2)
假设您已拥有刷新令牌,则在创建TokenResponse
var token = new TokenResponse {
AccessToken = access_token,
RefreshToken = refresh_token
};
<强> User Credentials 强>
UserCredential是一个使用访问令牌的线程安全帮助程序类 访问受保护的资源。 访问令牌通常会在到期后过期 1小时,如果您尝试使用它,将会出现错误。
UserCredential和AuthorizationCodeFlow自动处理 &#34;刷新&#34;令牌,这只是意味着获得一个新的访问令牌。 这是使用您接收的长期刷新令牌完成的 如果使用access_type = offline参数,则使用访问令牌 在授权代码流程中。
在大多数应用程序中,建议存储凭据的访问权限 永久存储中的令牌和刷新令牌。否则,你会的 需要在浏览器中向最终用户提供授权页面 每小时,因为访问令牌在您之后一小时到期 收到了。