- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在研究 Google Api,它旨在获得登录人员的圈子。我已经拥有访问 token ,但问题是每当我尝试运行我的代码它返回这个异常
The access token has expired but we can't refresh it
我该如何解决这个问题?
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();
}
最佳答案
假设您已经拥有刷新 token ,则在创建 TokenResponse
var token = new TokenResponse {
AccessToken = access_token,
RefreshToken = refresh_token
};
UserCredential is a thread-safe helper class for using an access token to access protected resources. An access token typically expires after 1 hour, after which you will get an error if you try to use it.
UserCredential and AuthorizationCodeFlow take care of automatically "refreshing" the token, which simply means getting a new access token. This is done using a long-lived refresh token, which you receive along with the access token if you use the access_type=offline parameter during the authorization code flow.
In most applications, it is advisable to store the credential's access token and refresh token in persistent storage. Otherwise, you will need to present the end user with an authorization page in the browser every hour, because the access token expires an hour after you've received it.
关于c# - 如何在 MVC 中解决 'The access token has expired but we can' t 刷新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38431281/
我是一名优秀的程序员,十分优秀!