【问题标题】:Getting "unauthorized" error when trying to refresh access token尝试刷新访问令牌时出现“未经授权”错误
【发布时间】:2018-08-22 07:54:32
【问题描述】:

我正在尝试为关注this tutorial 的用户刷新访问令牌。

但是,我得到了

{
    "error":"unauthorized",
    "error_description":"Full authentication is required to access this resource"
}

而且我看不到缺少什么。

以下是我在 Angular 应用程序中构造 oauth/refresh 请求的方式:

refreshToken() {

  this.logger.info('Attempting to refresh access token');

  const headers = new HttpHeaders()
    .set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
    // CLIENT_ID:CLIENT_SECRET
    .set('Authorization', 'Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ='); 

  const payload = {
    refresh_token: AuthenticationService.getRefreshToken(),
    grant_type: 'refresh_token'
  };

  return this.http.post(environment.apiUrl + '/oauth/refresh',
    payload, {headers: headers})
    .pipe(map(r => r));
}

我在这里错过了什么?

【问题讨论】:

    标签: spring spring-security oauth-2.0


    【解决方案1】:

    好吧,我几乎是对的。

    首先,我确实使用了错误的端点/oauth/refresh - 我不知道为什么我认为它存在。必须是/oauth/token

    还可以通过 URL 参数发送有效负载:

    const payload = `refresh_token=${AuthenticationService.getRefreshToken()}&grant_type=refresh_token`;
    

    所以最后我得到了这个:

    refreshToken() {
    
      this.logger.info('Attempting to refresh access token');
    
      const headers = new HttpHeaders()
        .set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
        .set('Authorization', 'Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=');
    
      const payload = `refresh_token=${AuthenticationService.getRefreshToken()}&grant_type=refresh_token`;
    
      return this.http.post(environment.apiUrl + '/oauth/token',
        payload, {headers: headers})
        .pipe(map(r => r));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 2011-09-15
      • 2021-09-11
      • 2021-11-07
      • 2020-12-31
      • 2015-02-22
      • 2016-06-02
      相关资源
      最近更新 更多