php - Pinterest API 未按预期工作

标签 php api curl pinterest

我正在尝试使用 PHP 使用 Pinterest API,但到目前为止它还没有真正起作用。 我使用本指南开始:

https://developers.pinterest.com/docs/api/authentication/

除了第 3 步,我现在一切正常。 文档说这只是一个简单的请求

https://api.pinterest.com/v1/me/?access_token=<YOUR-ACCESS-TOKEN>

  1. Making authenticated requests

Finally, once you have an access token, you can make authorized requests on the users behalf using OAuth 2.0 Bearer tokens supplied via the Authorization request header or via a request parameter named access_token (supplied as either a form-encoded body parameter or a URI query parameter). The Authorization header is preferred.

For example, you can visit this site on your browser:

https://api.pinterest.com/v1/me/?access_token= A sample response might look like:

{ "data": { "url": "https://www.pinterest.com/ben/", "first_name": "Ben", "last_name": "Silbermann", "id": "4788400174839062" } }

我有一个 access_token,看起来像这样:

AcFFayZKwqXuKVkj1J-0QN1fCob1FAgjuP9-OtFCg_j49UAgogXXXXX

但每次我收到这条消息时:

{"status": "failure", "code": 3, "host": "coreapp-devplatform-devapi-181", "generated_at": "Mon, 28 Sep 2015 12:28:38 +0000", "message": "Authorization failed.", "data": null}

我使用此 PHP 代码从 URL 获取数据:

        $curl = curl_init($url);
        curl_setopt_array($curl, array(
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_FORBID_REUSE => true,
            CURLOPT_FRESH_CONNECT => true,
            CURLOPT_HEADER => false,
            CURLOPT_HTTPHEADER => array("Content-Type: application/x-www-form-urlencoded"),
            CURLOPT_NOBODY => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 30,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_USERAGENT => self::userAgent()
        ));
        $result = curl_exec($curl);
        var_dump($result);
        $errorCode = curl_errno($curl);
        $respCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

最佳答案

我刚刚经历了同样的错误,但针对的是不同的端点。试试这段代码

        $ch = curl_init();

        $url = "https://api.pinterest.com/v1/me/?access_token='YOUR_ACCESS_TOKEN'";

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = trim(curl_exec($ch));
        curl_close($ch);

        print_r($result);  

关于php - Pinterest API 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823010/

相关文章:

php - 如何使用 PHP 在单个页面中使用两个不同的查询创建两个分页?

php - 如果用户被禁止,则终止事件 session

facebook graph api 评论列表排序,比如 'orderby=desc'?

python - 如何在 Google Compute Engine 上授权进入 BigQuery?

javascript - CORS header 'Access-Control-Allow-Origin' 仅在浏览器/jquery 中丢失,但可以使用 curl

java - Bing map 地理数据解压缩算法移植到 PHP

php - 检测当前用户是否有事件订阅

api - Swagger 是否有能力支持 API 更改历史记录,或者是否有开源库支持?

asp.net - 使用没有 API key 的谷歌地图?

php - 使用文件获取内容或 curl