How to resolve 'The access token has expired but we can't refresh it' in MVC

╄→尐↘猪︶ㄣ 提交于 2019-12-24 00:57:32

问题


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

}

回答1:


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.



来源:https://stackoverflow.com/questions/38431281/how-to-resolve-the-access-token-has-expired-but-we-cant-refresh-it-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!