1

他们的 API:http ://www.sitescout.com/support/api/#authentication 声明我应该向https://api.sitescout.com/oauth/token提交一个 POST 请求,并将 Authorization 标头设置为我自己的凭据( base64_encode(“用户名:密码”))。下面是一个示例请求:

POST https://api.sitescout.com/oauth/token HTTP/1.1
Host: api.sitescout.com
Authorization: Basic YmVldGhvdmVuOmxldG1laW4=
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Content-Length: 41

grant_type=client_credentials&scope=STATS

我应该得到这样的东西:

{

    "scope": "STATS",
    "access_token": "7ebe55b54ee12a8ee07329f1cefd6de6",
    "token_type": "bearer",
    "expires_in": 3600

}

我的代码:

  $url = "https://api.sitescout.com/oauth/token";
  $ch = curl_init();

  $headers  = array(
    "POST https://api.sitescout.com/oauth/token HTTP/1.1",
    "HOST: api.sitescout.com",
    "Authorization: Basic ZGlnaWZ1c2UtYXBpOnh1M2pkODll****",
    "Content-Type: application/x-www-form-urlencoded",
    "Accept: application/json",
    "Content-Length: 41"
  );

  $post_fields = array(
    'grant_type' => 'credentials'
  );

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

  $output = curl_exec($ch);
  //$info = curl_getinfo($ch);
  curl_close($ch);

  var_dump($output);

不管用。有人可以指出我正确的方向吗?多谢。

4

3 回答 3

1

Auth 标头应该像:

$header = array(
      "POST https://api.sitescout.com/oauth/token HTTP/1.1",
      "HOST: api.sitescout.com",
      "Authorization: {$this->_auth_header}",
      "Content-Type: application/x-www-form-urlencoded",
      "Accept: application/json",
      "Content-Length: 41"
    );

您还可以使用此 API Wrapper Class 来制作 Auth 和 fatch API 报告信息。它包括所有功能,以获取活动、网站、创意等。

https://github.com/lokeshpahal/sitescout

于 2014-05-31T06:07:40.260 回答
0

不确定您遇到了什么错误,但我认为根据您的示例代码,标头参数中一定有问题。“POST https://api.sitescout.com/oauth/token HTTP/1.1”不应成为 HTTP 标头的一部分。

于 2013-10-25T15:15:56.063 回答
0

Digifuse,你能让它工作吗?

如果不是,我认为问题应该出在这一部分:

'grant_type' => 'credentials'

实际上应该是:

'grant_type' => 'client_credentials'
于 2013-11-27T19:37:00.097 回答