2013-03-19 55 views
3

請注意Twitter應用程序的唯一身份驗證PHP OAuth的錯誤

完全實施工作的Twitter應用程序的唯一身份驗證的Oauth PHP庫現在可以在下面GitHub的鏈接中找到:Twitter Application Only Authentication OAuth Php


我試圖讓我的應用程序通過Twitter的應用程序唯一身份驗證進行身份驗證。在以下網址見文檔:

Twitter Developer Documentation fro Application-only authentication

我得到第2步,用粘貼下面的代碼請求建立承載令牌。我收到看起來像下面的響應:

HTTP/1.1 200 OK 
Date: Tue, 19 Mar 2013 15:13:03 GMT 
Status: 200 OK 
X-Frame-Options: DENY 
ETag: "6b7ec13d0ef1e9d8e0b39bec5266ba7b" 
X-Runtime: 0.06008 
Pragma: no-cache 
Last-Modified: Tue, 19 Mar 2013 15:13:03 GMT 
Content-Type: application/json; charset=utf-8 
X-Transaction: 55809f2187d74c8e 
X-MID: 4503f89925628df071cb1d27c3e6953709234b1b 
Expires: Tue, 31 Mar 1981 05:00:00 GMT 
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 
Set-Cookie: k=10.37.190.122.1363705983314067; path=/; expires=Tue, 26-Mar-13 15:13:03 GMT; domain=.twitter.com 
Set-Cookie: _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlZTI5MjJmNDI5ODY4MzM0NjAz%250AMzkwZTE2NDY5MzdiNGM6D2NyZWF0ZWRfYXRsKwhV8TWDPQE6B2lkIiU2NzEy%250AZjgzOTNjYjc4NjNhYzgwMjU2Mjc1Yzc0ZDYyMw%253D%253D--22b3f64a757c1f9f3c3ae8df9ee434848c43eee8; domain=.twitter.com; path=/; HttpOnly 
Vary: Accept-Encoding 
Content-Encoding: gzip 
Content-Length: 138 
Server: tfe 
Set-Cookie: guest_id=v1%3A136370598331525689; Domain=.twitter.com; Path=/; Expires=Thu, 19-Mar-2015 15:13:03 UTC 

‹«V*ÉÏNÍx‹/©,HU²RJJM,J-RÒQJLNN-.ŽKrű‚|sg(«¢Ê(Ô5*ÝÕÃÂGÕÈ­ÌÃxß²(ÐÄÙ(Í4ØÍÔDÕØ¥2+ȳԠԨÊ7«À«$,+ÛÃ"ß35 

正如你可以看到返回並不n要似乎回到一個JSON字符串的內容,如文檔

HTTP/1.1 200 OK 
Status: 200 OK 
Content-Type: application/json; charset=utf-8 
... 
Content-Encoding: gzip 
Content-Length: 140 

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"} 

描述我們可以看到

‹«V*ÉÏNÍx‹/©,HU²RJJM,J-RÒQJLNN-.ŽKrű‚|sg(«¢Ê(Ô5*ÝÕÃÂGÕÈ­ÌÃxß²(ÐÄÙ(Í4ØÍÔDÕØ¥2+ȳԠԨÊ7«À«$,+ÛÃ"ß35 

不等於

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"} 

我想知道如果這可能是一個可能的編碼錯誤,或者如果我從根本上做錯了cURL的東西。

我的代碼粘貼下面,任何幫助,將不勝感激:

喬恩

<?php 
// from https://dev.twitter.com/docs/auth/application-only-auth 
$consumer_key = 'myconsumerkey'; 
$consumer_secret = 'myconsumersecret'; 

// step 1 
    // step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 
    $encoded_consumer_key = urlencode($consumer_key); 
    $encoded_consumer_secret = urlencode($consumer_secret); 
    // step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secr et 
$bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; 
    // step 1.3 - base64-encode bearer token 
    $base64_encoded_bearer_token = base64_encode($bearer_token); 
// step 2 
     $url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication 
     $headers = array( 
      "POST /oauth2/token HTTP/1.1", 
      "Host: api.twitter.com", 
      "User-Agent: my Twitter App v.1", 
      //"Authorization: Basic ".$base64_encoded_bearer_token."", 
      "Authorization: Basic ".$base64_encoded_bearer_token."", 
      "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", 
      "Content-Length: 29", 
      "Accept-Encoding: gzip" 
     ); 

$ch = curl_init(); // setup a curl 
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers 
curl_setopt($ch, CURLOPT_POST, 1); // send as post 
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent 
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers 
//$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$result = curl_exec($ch); // run the curl 
curl_close($ch); // stop curling 
?> 
+3

我覺得'接受編碼:gzip '頭文件可能是你的問題 - 你說你接受一個gzip的迴應,但是你不知道該怎麼處理它......所以不要設置頭文件。 – CBroe 2013-03-19 15:42:54

+0

這就是問題所在:D感謝您的迴應 – jonhurlock 2013-03-19 15:51:18

+0

這純粹是爲了代碼的可讀性,但是當您使用'$ bearer_token'作爲'$ encoded_consumer_key。'的變量名:'。'encoded_consumer_secret'時,您實際上是_requesting_持有者令牌,所以更好的變量名稱將是'$ credentials'和'$ base64cred'。再一次,純粹爲了可讀性。 – inorganik 2013-09-16 16:44:38

回答

2

通過CBroe在評論中回答:

我認爲的Accept-Encoding:gzip頭可能是你的問題在這裏 - 你說你接受一個gzip的迴應,但是你不知道該怎麼做......所以不要設置這個頭文件。 - CBroe

2

正如CBroe在評論中所說,刪除標頭'Accept-Encoding:gzip'將會解決。

如果你仍然想使用gzip加快轉移,捲曲將添加頁眉和自動ungzip對你的反應,如果你設置的選項:

curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); 
相關問題