尝试刷新访问令牌时出现“未经授权”错误

时间:2018-08-21 11:31:00

标签: spring spring-security oauth-2.0

我正在尝试为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));
}

我在这里想念什么?

1 个答案:

答案 0 :(得分: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));
}