1

正如标题所述,每当我尝试从 web 服务 API 请求任何端点时,我都会收到 401 响应(我正在运行 orocrm 4.1,并且生成了公钥 + 私钥并以正确的名称放置在 /var 中)。

1. 创建 OAuth 应用程序

授予类型 = client_credentials(所选用户拥有所有管理员权限): 在此处输入图像描述

2.获取token(使用GuzzleHttp客户端)

$base_uri = 'https://mywebsite.com';
$generate_token_endpoint = '/oauth2-token';
$grant_type = 'client_credentials';
$client_id = '###############################';
$client_secret = '#################################';

$client = new Client(array(
    'base_uri' => $base_uri,
     'headers' => array(
          'Content-Type' => 'application/vnd.api+json'
      )
 ));

$response = $client->post($generate_token_endpoint, array(
    'form_params' => array(
          'grant_type' => $grant_type,
          'client_id' => $client_id,
          'client_secret' => $client_secret
     )
));

$tokenJson = json_decode($response->getBody()->getContents(), true);
$token = 'Bearer '.$tokenJson['access_token'];

// The actual request to get some data
$response = $client->get('/api/users', array(
     'headers' => array(
          'Authorization' => $token
      )
));

我确实收到了一个令牌,我什至延长了它的生命周期,但是下一个请求的结果是(无论我做什么):

Client error: 'GET https://mywebsite.com/api/users' resulted in a '401 Unauthorized' response

我也尝试过使用 POSTMAN,但结果是一样的。

帮助 !

4

2 回答 2

1

请生成 public.key 或 private.key 并存储到 var 文件夹,如:

app folder / var / oauth_private.key
app folder / var / oauth_public.key

您可以按照以下步骤操作:

  1. openssl genrsa -out oauth_private.key 2048
  2. openssl rsa -in private.key -pubout -out oauth_public.key
  3. 然后移动到 var 文件夹

参考链接

https://oauth2.thephpleague.com/installation/#generating-public-and-private-keys
于 2020-05-01T07:57:21.637 回答
0

请确保您正确生成了加密密钥并将它们放置在具有适当名称的 var 文件夹中:https ://doc.oroinc.com/user/back-office/system/user-management/oauth-app/#starting-conditions 当密钥配置不正确时,在带有 oAuth 应用程序的页面上,您将看到警告

OAuth 授权不可用,因为加密密钥配置未完成。请联系您的管理员。

如果密钥在那里,您必须在身份验证后检查应用程序日志是否有错误。另外,检查来自服务器的响应。通常,它有一些关于什么是错误的提示。

于 2020-03-10T13:52:17.937 回答