简体   繁体   English

Nginx 401 与 PHP cURL

[英]Nginx 401 with PHP cURL

In order to get an oAuth2 token, I must connect to a REST API at this URL: https://abcd/api/oauth/v1/token by sending base64_encode(api_client_id:api_secret) .为了获得 oAuth2 令牌,我必须通过发送base64_encode(api_client_id:api_secret)连接到 REST API URL: https://abcd/api/oauth/v1/token Note that moreover, the access to https://abcd/api/oauth/v1/token is protected by a htpwd.此外,请注意,对https://abcd/api/oauth/v1/token的访问受 htpwd 保护。

So my request, written in PHP, is:所以我写在 PHP 的请求是:

$base64_encoded_client_id_and_secret = base64_encode('api_client_id:api_secret');
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, 'https://abcd/api/oauth/v1/token');
curl_setopt($curl_session, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Authorization: Basic ' . $base64_encoded_client_id_and_secret]);
curl_setopt($curl_session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl_session, CURLOPT_USERPWD, "htpwd_user:htpwd_pwd");
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, [
    'grant_type' => 'password',
    'username'  =>  'api_user',
    'password'  =>  'api_pwd'
    ]);
$ret = json_decode(curl_exec($curl_session));

However the Nginx server returns the error 401. What could I do to fix this bug?但是 Nginx 服务器返回错误 401。我该怎么做才能修复此错误?

I have tested different values instead of CURLAUTH_DIGEST ;我测试了不同的值而不是CURLAUTH_DIGEST none worked.没有工作。

Is it a CROOS Origin problem?是 CROOS Origin 的问题吗?

The following solution is not a good one even if it works (read the following).以下解决方案即使有效(请阅读以下内容)也不是一个好的解决方案。 The problem was solved by removing the htpasswd security step for an arbitrary period of time (equal to the sum of my test and development time).通过在任意时间段内删除 htpasswd 安全步骤(等于我的测试和开发时间的总和)解决了这个问题。 Be careful if you do this, because Google could possibly index the website from a moment belonging to this time interval.如果你这样做要小心,因为谷歌可能会从属于这个时间间隔的时刻开始索引网站。

I didn't try to separate the field values with a comma as proposed by @AngelDeykov in a comment (not in an answer).我没有尝试按照@AngelDeykov 在评论中(不是在答案中)提出的那样用逗号分隔字段值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM