Im sử dụng mã sau để đọc tới consumer_key
và consumer_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?
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