如何解决“访问令牌已过期,但我们不能刷新它'在MVC [英] How to resolve 'The access token has expired but we can't refresh it' in MVC

查看:8052
本文介绍了如何解决“访问令牌已过期,但我们不能刷新它'在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的谷歌阿比其目的在于获得的loggedIn person.i中的圈已经在访问令牌但问题是,每当我尝试我的代码运行它返回此异常




访问令牌已过期,但我们不能刷新




我该如何解决这个问题?

  VAR claimsforUser =等待UserManager.GetClaimsAsync(User.Identity.GetUserId()); 
变种的access_token = claimsforUser.FirstOrDefault(X => x.Type ==瓮:谷歌:的accessToken)。值;

的String [] =范围新的String [] {PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile};

VAR流量=新GoogleAuthorizationCodeFlow(新GoogleAuthorizationCodeFlow.Initializer
{

ClientSecrets =新ClientSecrets
{
客户端Id =XX-XX .apps.googleusercontent.com,
ClientSecret =v-XX,
},
作用域=范围,
的DataStore =新FileDataStore(存储),
});

变种令牌=新TokenResponse {=的accessToken的access_token,ExpiresInSeconds = 50000};
无功证书=新UserCredential(流量,Environment.UserName,令牌);


PlusService服务=新PlusService(新BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
应用程序名称=ArcaneChatV2,
});

PeopleResource.ListRequest listPeople = service.People.List(我,PeopleResource.ListRequest.CollectionEnum.Visible);
listPeople.MaxResults = 10;
PeopleFeed peopleFeed = listPeople.Execute();
变种人=新的List<&人GT;();


,而(peopleFeed.Items!= NULL)
{

的foreach(人在peopleFeed.Items项)
{
people.Add(项目);
}
如果(peopleFeed.NextPageToken == NULL)
{
中断;
}
listPeople.PageToken = peopleFeed.NextPageToken;

//执行并处理下一个页面请求
peopleFeed = listPeople.Execute();

}


解决方案

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



<预类=郎-CS prettyprint -override> 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.

这篇关于如何解决“访问令牌已过期,但我们不能刷新它'在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆