首页 > 解决方案 > 尝试刷新访问令牌时出现“未经授权”错误

问题描述

我正在尝试按照本教程刷新用户的访问令牌。

但是,我越来越

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

而且我看不到缺少什么。

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

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

我在这里想念什么?

标签: springspring-securityoauth-2.0

解决方案


好吧,我几乎是对的。

首先,我确实使用了错误的端点/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));
}

推荐阅读