且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决“访问令牌已过期,但我们不能刷新它'在MVC

更新时间:2023-02-16 09:49:01

假设你已经拥有了刷新令牌,你包括刷新令牌,当你创建 TokenResponse



VAR令牌=新TokenResponse {
的accessToken =的access_token,
RefreshToken = refresh_token
};



用户凭据




UserCredential是使用一个访问令牌
访问受保护的资源的一个线程安全辅助类。 的访问后,
1个小时,之后如果您尝试使用它



UserCredential,你会得到一个错误标记通常过期和AuthorizationCodeFlow照顾自动
提神的道理,这只是意味着获得新的访问令牌。
这是使用长寿命刷新令牌,你沿着
收到访问令牌,如果你的授权码流过程中使用ACCESS_TYPE =离线参数
完成。



在大多数应用中,***是存储凭证的访问
令牌并在持久存储器刷新令牌。否则,你将你
收到后,
需要在浏览器中
每隔一小时一个授权页面呈现的最终用户,因为访问令牌过期一个小时。


块引用>

I'm currently working on Google Api which aims to get the circles of a loggedin person.I already have the access token but the problem is whenever I try to run my code it returns this exception

The access token has expired but we can't refresh it

How do I resolve this issue?

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();

}

Assuming you already have the refresh token, you include the refresh token when you create the TokenResponse

var token = new TokenResponse { 
    AccessToken = access_token, 
    RefreshToken = refresh_token
};

User Credentials

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.