2013-10-24 124 views
1

他們的API:http://www.sitescout.com/support/api/#authentication 指出我應該提交POST請求到https://api.sitescout.com/oauth/token,授權標頭設置爲我自己的憑據(base64_encode(「username:password」))。下面是一個例子請求:SiteScout oauth2身份驗證

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); 

不工作。有人能請我指出正確的方向嗎?非常感謝。

回答

0

不知道你遇到了什麼錯誤,但是我認爲根據你的示例代碼,在頭部參數中肯定有問題。 「POST https://api.sitescout.com/oauth/token HTTP/1.1」不應該是HTTP標頭的一部分。

0

Digifuse,你是否能夠使它工作?

如果不是這樣,我相信這個問題應該是這一部分:

'grant_type' => 'credentials' 

這實際上應該是:

'grant_type' => 'client_credentials' 
1

驗證頭想:

$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包裝類進行Auth和fatch API報告信息。 它包括所有功能混帳系列,網站,廣告內容等。

https://github.com/lokeshpahal/sitescout

+0

我得到錯誤「提供無效的訪問令牌」,你能幫幫我嗎? – s4suryapal

+0

似乎提供的訪問令牌提取的信息不正確,您是如何獲取Access令牌的? – lokeshpahal

+0

我使用你的代碼:「https://github.com/lokeshpahal/sitescout".I只需在創建對象時傳遞我的憑據,如下所示:」$ api = new API('CLIENT_ID','CLIENT_SECRET' );」還有什麼我必須設置? – s4suryapal