TwitterAPIExchange:大量媒体上传:STATUS需要身份验证

时间:2018-09-03 23:57:17

标签: php twitter twitter-oauth

此独立的PHP,TwitterAPIExchange代码尝试使用Chunks将文件文件上传到Twitter。除了STATUS部分(您必须执行INIT,APPEND,FINALIZE并可以检查其STATUS),所有这些似乎都可以正常工作

string(64) "{"errors":[{"code":32,"message":"Could not authenticate you."}]}" Response after check STATUS: end

这似乎不会影响上传,但是我不明白为什么这四个命令之一不起作用?有任何想法/建议吗?

在本文的结尾,我包含了此代码段的输出(注意:APPEND不返回响应-根据API文档,这是正确的,该API文档显示了每个命令的各种响应和var_dump

顺便说一句,最终的tweet起作用了,但是没有显示图像,但这是一个持续存在的问题,我已经在其他地方寻求帮助,但是也欢迎对此有任何见解。

require_once($_SERVER['DOCUMENT_ROOT'] . '/php/classes/' . 'TwitterAPIExchange.php');

$settings = array(
'oauth_access_token' => "XXX",
'oauth_access_token_secret' => "XXX",
'consumer_key' => "XXX",
'consumer_secret' => "XXX");
$twitter = new TwitterAPIExchange($settings);



display('<br />============================= Image Details =========================================================<br />');
$image = $_SERVER['DOCUMENT_ROOT'] . '/soundbites/images/gxk.jpg';
$fileSize = filesize($image);   
display($image . ': Size is ' . $fileSize . ' bytes');



display('<br />=============================== INIT =======================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';

$postfields = array('command' => 'INIT', 'total_bytes' => $fileSize, 'media_type' => 'image/jpeg');

display('<br />postfields after INIT: start<br />');
var_dump($postfields);
display('<br />postfields after INIT: end<br />');

$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

display('<br />Response after INIT: start<br />');
var_dump($response);
display('<br />Response after INIT: end<br />');
$media_id = json_decode($response)->media_id;
display("Media_id: " . $media_id);



display('<br />================================ APPEND ======================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';
    $postfields = array('command' => 'APPEND',
                'media_id' => $media_id,
                'media_data' => base64_encode(file_get_contents($image)),
                //media' => $image,
                'segment_index' => 0);

display('<br />postfields after APPEND: start<br />');
//var_dump($postfields); // Too large to display here, but outputs the file
display('<br />postfields after APPEND: end<br />');

    $response = $twitter->buildOauth($url, $requestMethod)
                ->setPostfields($postfields)
                ->performRequest();


    display('Response after APPEND: start');
    var_dump($response);
    display('Response after APPEND: end');


display('<br />========================== STATUS ============================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'GET';

$postfields = array('command' => 'STATUS', 'media_id' => $media_id);


display('<br />postfields after check STATUS: start<br />');
var_dump($postfields);
display('<br />postfields after check STATUS: end<br />');

$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

display('Response after check STATUS: start');
var_dump($response);
display('Response after check STATUS: end');





display('<br />============================== FINALIZE ========================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';

$postfields = array('command' => 'FINALIZE',
                    'media_id' => $media_id);

display('<br />postfields after FINALIZE: start<br />');
var_dump($postfields);
display('<br />postfields after FINALIZE: end<br />');

$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

display('Response after FINALIZE: start');
var_dump($response);
display('Response after FINALIZE: end');



display('<br />=============================== TWEET =======================================================<br />');
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';

$postfields = array(
  'status' => 'Test Tweet - Delete me',
  'media_id_string' => $media_id);

display('<br />postfields after TWEET: start<br />');
var_dump($postfields);
display('<br />postfields after TWEET: end<br />');


    $response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

display('Response after TWEET: start');
var_dump($response);
display('Response after TWEET: end');


function display ($msg = "No custom message", $fn = "No fn defined"){
    echo $msg . "<br />";
}

===输出===

============================= Image Details =========================================================
/home2/sentralg/public_html/soundbites/images/gxk.jpg: Size is 42665 bytes

=============================== INIT =======================================================


postfields after INIT: start

array(3) { ["command"]=> string(4) "INIT" ["total_bytes"]=> int(42665) ["media_type"]=> string(10) "image/jpeg" }
postfields after INIT: end

Response after INIT: start

string(99) "{"media_id":1036759321785163777,"media_id_string":"1036759321785163777","expires_after_secs":86400}"
Response after INIT: end

Media_id: 1036759321785163777

================================ APPEND ======================================================


postfields after APPEND: start


postfields after APPEND: end

Response after APPEND: start
string(0) "" Response after APPEND: end

========================== STATUS ============================================================


postfields after check STATUS: start

array(2) { ["command"]=> string(6) "STATUS" ["media_id"]=> int(1036759321785163777) }
postfields after check STATUS: end

Response after check STATUS: start
string(64) "{"errors":[{"code":32,"message":"Could not authenticate you."}]}" Response after check STATUS: end

============================== FINALIZE ========================================================


postfields after FINALIZE: start

array(2) { ["command"]=> string(8) "FINALIZE" ["media_id"]=> int(1036759321785163777) }
postfields after FINALIZE: end

Response after FINALIZE: start
string(165) "{"media_id":1036759321785163777,"media_id_string":"1036759321785163777","size":42665,"expires_after_secs":86400,"image":{"image_type":"image\/jpeg","w":200,"h":147}}" Response after FINALIZE: end

=============================== TWEET =======================================================


postfields after TWEET: start

array(2) { ["status"]=> string(22) "Test Tweet - Delete me" ["media_id_string"]=> int(1036759321785163777) }
postfields after TWEET: end

Response after TWEET: start
string(2462) "{"created_at":"Mon Sep 03 23:34:20 +0000 2018","id":1036759326759636992,"id_str":"1036759326759636992","text":"Test Tweet - Delete me","truncated":false," ... [remainder deleted]
Response after TWEET: end

0 个答案:

没有答案