2011-07-13 24 views
5

Im sử dụng mã sau để đọc tới consumer_keyconsumer_secret từ config.php, chuyển nó lên twitter và truy xuất một số thông tin từ chúng.PHP/Twitter oAuth - Tweet tự động

Kịch bản bên dưới cố gắng làm là 'cache' request_token và request_secret. Vì vậy, trong lý thuyết tôi sẽ có thể tái sử dụng những chi tiết đó (tất cả 4 người trong số họ sẽ tự động tweet khi được yêu cầu).

<?php 

require_once('twitteroauth/twitteroauth.php'); 
require_once('config.php'); 

    $consumer_key = CONSUMER_KEY; 
    $consumer_secret = CONSUMER_SECRET; 

if (isset($_GET["register"])) 
{ 
    // If the "register" parameter is set we create a new TwitterOAuth object 
    // and request a token 

    /* Build TwitterOAuth object with client credentials. */ 

    $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); 
    $request = $oauth->getRequestToken(); 

    $request_token = $request["oauth_token"]; 
    $request_token_secret = $request["oauth_token_secret"]; 

    // At this I store the two request tokens somewhere. 

    file_put_contents("request_token", $request_token); 
    file_put_contents("request_token_secret", $request_token_secret); 

    // Generate a request link and output it 
    $request_link = $oauth->getAuthorizeURL($request); 
    echo "Request here: <a href=\"" . $request_link . "\">" . $request_link . "</a>"; 
    die(); 
} 
elseif (isset($_GET["validate"])) 
{ 
    // This is the validation part. I read the stored request 
    // tokens. 

    $request_token = file_get_contents("request_token"); 
    $request_token_secret = file_get_contents("request_token_secret"); 

    // Initiate a new TwitterOAuth object. This time we provide them with more details: 
    // The request token and the request token secret 

    $oauth = new TwitterOAuth($consumer_key, $consumer_secret, 
     $request_token, $request_token_secret); 

    // Ask Twitter for an access token (and an access token secret) 
    $request = $oauth->getAccessToken(); 

    // There we go 
    $access_token = $request['oauth_token']; 
    $access_token_secret = $request['oauth_token_secret']; 

    // Now store the two tokens into another file (or database or whatever): 
    file_put_contents("access_token", $access_token); 
    file_put_contents("access_token_secret", $access_token_secret); 

    // Great! Now we've got the access tokens stored. 
    // Let's verify credentials and output the username. 
    // Note that this time we're passing TwitterOAuth the access tokens. 


    $oauth = new TwitterOAuth($consumer_key, $consumer_secret, 
     $access_token, $access_token_secret); 

    // Send an API request to verify credentials 
    $credentials = $oauth->oAuthRequest('https://twitter.com/account/verify_credentials.xml', 'GET', array()); 

    // Parse the result (assuming you've got simplexml installed) 
    $credentials = simplexml_load_string($credentials); 

    var_dump($credentials); 

    // And finaly output some text 
    echo "Access token saved! Authorized as @" . $credentials->screen_name; 
    die(); 
} 
?> 

Khi tôi chạy /?verify&oauth_token=0000000000000000 - Nó hoạt động tuy nhiên cố gắng để resuse các thẻ tạo vv ... tôi nhận được một 401

Dưới đây là bit cuối cùng của mã nơi tôi cố gắng để tái sử dụng các chi tiết từ Twitter kết hợp với consumer_key và c onsumer_secret và nhận được 401 của tôi: chắc chắn

require_once('twitteroauth/twitteroauth.php'); 
require_once('config.php'); 

// Read the access tokens 
$access_token = file_get_contents("access_token"); 
$access_token_secret = file_get_contents("access_token_secret"); 

// Initiate a TwitterOAuth using those access tokens 
$oauth = new TwitterOAuth($consumer_key, $consumer_key_secret, 
    $access_token, $access_token_secret); 

// Post an update to Twitter via your application: 
$oauth->OAuthRequest('https://twitter.com/statuses/update.xml', 
    array('status' => "Hey! I'm posting via #OAuth!"), 'POST'); 

Không whats đi sai, bạn có thể để cache các chi tiết hoặc làm tôi cần phải thử cái gì khác?

+0

Bạn có đặt $ consumer_key và $ consumer_key_secret từ định nghĩa trong cấu hình trong tập lệnh thứ hai của mình không? – GregSchoen

Trả lời

5

Bạn không thể lưu trữ mã thông báo OAuth trong bộ nhớ cache và nhiều hơn 1 yêu cầu, vì OAuth ở đó để giúp bảo mật hệ thống, "oauth_token" của bạn sẽ chứa một số dữ liệu duy nhất, mã thông báo này sẽ chỉ có thể để thực hiện một cuộc gọi trở lại twitter, ngay khi cuộc gọi được thực hiện, "oauth_token" không còn hợp lệ và lớp OAuth sẽ yêu cầu "oauth_token" mới, do đó đảm bảo mọi cuộc gọi được thực hiện đều an toàn.

Đó là lý do tại sao bạn nhận được lỗi "401 trái phép" vào lần thứ hai vì mã thông báo không còn giá trị.

twitter vẫn đang sử dụng OAuth v1 (v2 vẫn đang trong quá trình soạn thảo mặc dù facebook và google đã triển khai nó ở một số phần) Hình ảnh dưới đây mô tả luồng xác thực OAuth. Hy vọng điều đó sẽ hữu ích.

OAuth authentication flow

Một thời gian trước, tôi sử dụng này để kết nối với twitter và gửi tweets, chỉ cần lưu ý rằng nó đã làm cho sử dụng một số lớp học Zend như dự án đã được chạy trên một máy chủ zend.

require_once 'Zend/Service/Twitter.php'; 
class Twitter { 

    protected $_username = '<your_twitter_username>'; 
    protected $_token = '<your_twitter_access_token>'; 
    protected $_secret = '<your_twitter_access_token_secret>'; 
    protected $_twitter = NULL; 

    //class constructor 
    public function __construct() { 
     $this->getTwitter(); 
    } 

    //singleton twitter object 
    protected function getTwitter() { 
     if (null === $this->_twitter) { 
      $accessToken = new Zend_Oauth_Token_Access; 
      $accessToken->setToken($this->_token) 
        ->setTokenSecret($this->_secret); 

      $this->_twitter = new Zend_Service_Twitter(array(
         'username' => $this->_username, 
         'accessToken' => $accessToken, 
        )); 

      $response = $this->_twitter->account->verifyCredentials(); 
      if ($response->isError()) { 
       throw new Zend_Exception('Provided credentials for Twitter log writer are wrong'); 
      } 
     } 
     return $this->_twitter; 
    } 

    //send a status message to twitter 
    public function update($tweet) { 
     $this->getTwitter()->status->update($tweet); 
    } 

} 
+0

Nhưng tôi nghĩ rằng tôi có thể cho phép tài khoản của tôi với ứng dụng khi tôi thiết lập nó tại dev.twitter.com/apps và nó tạo ra một mã thông báo truy cập và một bí mật mã thông báo truy cập. Tôi cũng đã thử nó và tôi nhận được một 401 mà nên làm việc. – CLiown

+0

Tôi đã thêm một số mã mẫu mà tôi đã sử dụng lần cuối để tạo ứng dụng twitter, hy vọng nó sẽ giúp – Stephan

0

Trong tập lệnh thứ hai, có vẻ như bạn không đặt Khóa người tiêu dùng và Bí mật người tiêu dùng khi tạo cá thể TwitterOAuth của mình.

// Initiate a TwitterOAuth using those access tokens 
$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret); 
Các vấn đề liên quan