0

能够读取用户配置文件并可以在我的 Angular 2 应用程序中从 Auth0 更新用户元数据。像这样

 this.lock.getProfile(authResult.idToken, (error, profile) => {
        if (error) {
          // Handle error
          alert(error);
          return;
        }

this.authHttp
      .patch('https://' + myConfig.domain + '/api/v2/users/' + this.auth.userProfile.user_id, data, {headers: headers})
      .map(response => response.json())
      .subscribe(
        response => {
          this.auth.userProfile = response;
          localStorage.setItem('profile', JSON.stringify(response));
          this.router.navigate(['/profile']);
        },
        error => alert(error.json().message)
      );

但是在试图让所有用户得到错误的同时 -"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404." 在此处输入图像描述

这是代码

var headers: any = {
      "Content-Type": "application/json; charset=utf-8",
      "cache-control": "no-cache"
    };
    this.authHttp
          .get('https://' + myConfig.domain + '/api/v2/users/' + '?per_page=100&page=0&search_engine=v2', { headers: headers })
          .map(response => response.json())
          .subscribe(
          response => {
            console.log(response);
          },
          error => alert(error.json().message)
          );

网站上的客户端测试运行良好 https://auth0.com/docs/api/management/v2#!/Users/get_users 在此处输入图像描述 不确定出了什么问题。

带有 Access-Control-Allow-Origin 的标头也有同样的问题

var headers: any = {
      "Content-Type": "application/json; charset=utf-8",
      "cache-control": "no-cache",
       "Access-Control-Allow-Origin": "*"
    };

在此处输入图像描述 在此处输入图像描述

4

3 回答 3

0

问题是即使不会发出 CORS 飞行前请求,请求也会失败。表明这一点的关键部分是对预检请求的响应不在2XX范围内。

您在404预检时收到了响应,但如果您从未绑定到 CORS 的应用程序发出相同的请求,您仍然会收到404.

您尝试调用的端点的正确地址是

https://[AUTH0_ACCOUNT]/api/v2/users

而不是那个,你打电话给:

https://[AUTH0_ACCOUNT]/api/v2/users/

您需要在末尾删除多余的斜线


但是,这将解决 404 问题,但不能解决根本问题...... Auth0 管理 API 旨在供服务器端应用程序使用,该应用程序可以通过客户端身份验证获取必要的访问令牌。

有关参考,请参阅Auth0 管理 APIv2 令牌

PATCH 用户端点起作用的原因可能是由于该端点仅影响单个用户并且允许使用 CORS。

于 2016-12-21T09:15:38.207 回答
0

实际上,您正在尝试使用localhost,因此您必须添加header后端:

 .header("Access-Control-Allow-Origin", "*")

希望这有帮助。

于 2016-12-20T17:39:02.603 回答
0

您在服务器(例如:Apache)存档中配置它:.htaccess与:

Header set Access-Control-Allow-Origin "*"

但是你也可以尝试在谷歌浏览器中安装扩展程序在开发环境的情况下:

输入网址:chrome://settings/-> Extensions-> Get more extensions-> 搜索Allow-Control-Allow-Origin: *-> Click to Active the Extension and try run code again

你可以看到为什么这个错误->点击

其他解决了几个人出现相同错误的问题的帖子->(stackoverflow

于 2016-12-20T17:49:01.737 回答