具有角度误差的github Oauth

时间:2017-12-31 18:38:17

标签: angular github oauth cors

getUserFromGithub() {
    const client_id = 'c****************c';
    const redirect_url = 'http://localhost:4200/oauth2/github';
    window.location.href = this.github_redirect_url + 'authorize?client_id='+ client_id+'&redirect_uri='+redirect_url+'&response_type=code&scope=*';
  }

以上角度方法重定向到github进行身份验证。

getRequest(request_code) {
      const body = JSON.stringify({
      'grant_type': 'authorization_code',
      'client_id': 'c******************c',
      'client_secret': 'e**************************************a',
      'redirect_uri': 'http://localhost:4200/oauth2/github',
      'code': request_code
    });
    const headers = new Headers({'Content-Type': 'application/json', 'Action': 'application/json'});
    return this.http.post('https://github.com/login/oauth/access_token', body,
        { headers: headers});
  }

然后github重定向回来,回复:

http://localhost:4200/oauth2/github
带有代码和重定向地址的

如下所示:

http://localhost:4200/oauth2/github?code=4709c6db720132e39f50

来自请求的此代码由 getRequest() 选取,并在

处发布帖子请求
https://github.com/login/oauth/access_token

使用必需的标头和代码(4709c6db720132e39f50)获取进一步用于授权的令牌。

现在问题来自此帖子请求。即使github使用代码成功重定向回 localhost:4200 ,但是对github的进一步发布请求无法返回任何响应并说: Access-Control-Allow-Origin Error

以上是截图。 问题是什么。 我无法理解。 我没有访问后端,因此我可以导入任何设置Access-Control-Allow-Origin的软件包。我能做什么。 我很麻烦。

0 个答案:

没有答案