php - 如何使用 file_get_contents() 向 HTTP 请求添加授权 header ?

标签 php api http

正如标题所说,我在为受 OAuth 1.0a 保护的文件发出签名 HTTP 请求时遇到问题。 我正在尝试将 file_get_contents 方法用于请求,并希望使用 stream_context_create() 方法添加授权 header 。 [下面的源代码]

$url = "https://rest.immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate";
    $context = stream_context_create(array(
        'http' => array(
            'header'  => "Authorization: OAuth
            oauth_consumer_key = \"MY_KEY\”,
            oauth_token = \"MY_TOKEN\"
            oauth_signature_method= \"HMAC-SHA1\",
            oauth_timestamp=\"TIMESTAMP\",
            oauth_nonce=\"MY_NONCE\",
            oauth_signature=\"MY_SIGNATURE\""
        )
    ));
    $data = file_get_contents($url, false, $context);

请求应该是这样的

GET /api/file HTTP/1.1 Host: example.com
Authorization: OAuth oauth_consumer_key="KEY",
    oauth_token="TOKEN",
    oauth_signature_method="HMAC-SHA1",
    oauth_timestamp="TIMESTAMP",
    oauth_nonce="NONCE",
    oauth_signature="SIGNATURE"

问题:我总是收到以下错误: 无法打开流:HTTP 请求失败! HTTP/1.1 401 在第 XX 行未经授权。 我究竟做错了什么?它正在创建标题吗? 我知道这也可以用 curl 来完成,但我实际上没有使用 curl 的知识和经验。

感谢您的帮助!

最佳答案

根据OAuth 1.0a spec

The OAuth Protocol Parameters are sent in the Authorization header the following way:

  1. Parameter names and values are encoded per Parameter Encoding.

  2. For each parameter, the name is immediately followed by an '=' character (ASCII code 61), a '"' character (ASCII code 34), the parameter value (MAY be empty), and another '"' character (ASCII code 34).

  3. Parameters are separated by a comma character (ASCII code 44) and OPTIONAL linear whitespace per [RFC2617].

  4. The OPTIONAL realm parameter is added and interpreted per [RFC2617], section 1.2.

一个问题是您将 \n (LF) 字符插入到 header 字符串中,根据 RFC2617,这是不允许的。在 oauth_token 之后,您似乎还缺少一个逗号。也不清楚您是否对参数进行了正确编码。

我认为避免犯这些错误的更简单方法是使用类似 http_build_query 的方法。并传递 PHP_QUERY_RFC3986 作为 enc_type 参数以符合 RFC3986 ,这是 OAuth 1.0a 声明的内容,或者您​​可以将参数设置在单独的数组中,然后 array_map自己编码。

$params = [
    "realm"                  => $realm, /* optional */
    "oauth_consumer_key"     => $key,
    "oauth_token"            => $token,
    "oauth_signature_method" => $sigmeth,
    "oauth_timestamp"        => $timestamp,
    "oauth_nonce"            => $nonce,
    "oauth_signature"        => $sig,
];

选项1

/* This will give you the proper encoded string to include in your Authorization header */
$params = http_build_query($params, null, ',', PHP_QUERY_RFC3986);    

$opts = ["http" => ["header" => "Authorization: OAuth " . $params]];

$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);

选项2

$params = implode(',',array_map(function($v,$k) {
    return $k . '="' . rawurlencode($v) . '"';
}, $params, array_keys($params)));

$opts = ["http" => ["header" => "Authorization: OAuth " . $params]];

$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);

我认为选项 2 实际上更符合 OAuth 1,因为 http_build_query 不会引用参数。 rawurlencode将符合 RFC3986,这应该有助于保持您的值正确编码。

关于php - 如何使用 file_get_contents() 向 HTTP 请求添加授权 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192321/

相关文章:

python - sqlalchemy 出现神秘错误 : TypeError 'User' object is not callable

java - 如何从 PHP 服务器上的 android 获取值

java - 如何从内部服务器在 Java Web 应用程序中提供静态内容?

php - Doctrine 模型数组数据类型

PHP循环替换字符串 "one-by-one"中的字符

php - 在动态导航 PHP 上突出显示当前页面

php - SOAP PHP 调用.NET。无参数

java - 没有 IllegalArgumentException 的 Apache commons PredicatedList

php - 如何正确保护 api key ?

flash - 关于使用 flash 或 java servlet 将麦克风数据从浏览器上传到服务器的教程?