Twitter为auth-only应用程序返回奇怪的数据

时间:2013-12-20 13:58:14

标签: php authentication twitter

我使用此代码

define('CONSUMER_KEY', 'X________________w');
define('CONSUMER_SECRET', 'k_______________________________A');
$encodedAccessToken = base64_encode(urlencode(CONSUMER_KEY).':'.urlencode(CONSUMER_SECRET));
$options = array(
"https" => array(
    "method" => "POST",
    "header" => "Host: api.twitter.com\n".
                 "User-agent: orlec_test v1.0.23\n".
                 "Authorization: Basic ".$encodedAccessToken."\n".
                 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8\n".
                 "Content-Length: 29\n".
                 "Accept-Encoding: gzip\n",
    "content" => "grant_type=client_credentials"
),
);
$url = "https://api.twitter.com/oauth2/token";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;

如果在$ options数组中使用“http”而不是twitter返回我:

ЋV*ЩЯNЭ/Љ,HUВRJJM,J-RвQJLNN-.KХБДаt+й ;д%ЪЗиЛТЌ бйТг5аЕАЂЪв7Џ$[еиХ7'8е9АРЫ5%8Ф%ЅАЪбЬа45 Ъ198ЂТФиЗ8/М(Г,2-ийХЧФШлаЯ+LЉэќ(

否则如果我使用“https”twitter return

Warning: file_get_contents(https://api.twitter.com/oauth2/token): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in C:\xampp\htdocs\index3.php on line 19

我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是app-only auth:https://gist.github.com/lgladdy/5141615

的工作示例