【问题标题】:Twitter API application auth only returns forbiddenTwitter API 应用程序身份验证仅返回禁止
【发布时间】:2013-03-08 05:28:07
【问题描述】:

我正在尝试了解如何将 oAuth2 与仅 twitter 应用程序一起使用身份验证,以便我可以在我的网站上显示提要,但我被困在令牌获取步骤中。我可能做得不对,我根本无法理解 oAuth 所以这是我的代码,你们能告诉我我做错了什么吗?

//Expired, request a bearer access token
define('TWITTER_CONSUMER_KEY', 'xyz');
define('TWITTER_CONSUMER_SECRET', 'abc');
$encodedAccessToken = base64_encode(TWITTER_CONSUMER_KEY.':'.TWITTER_CONSUMER_SECRET);

$options = array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Authorization: Basic '.$encodedAccessToken."\nContent-Type: application/x-www-form-urlencoded;charset=UTF-8",
        'content' => 'grant_type=client_credentials',
        'protocol_version' => 1.1,
    ),
);
$context  = stream_context_create($opts);
$result = file_get_contents('https://api.twitter.com/oauth2/token', false, $context);
var_dump($result);

然后我不断收到 403 错误:

警告:file_get_contents(https://api.twitter.com/oauth2/token) [function.file-get-contents]:无法打开流:HTTP 请求 失败的! HTTP/1.0 403 Forbidden in ...

【问题讨论】:

  • 请注意,我也尝试不使用 content-type 标头,并且不使用 http 1.1 版本
  • stream_context_create 得到一个未定义的变量$opts
  • 太棒了,我现在看起来像个傻瓜...发布答案,这样我就可以相信你了...我感觉很糟糕...
  • 在开发过程中停止抑制警告,error_reporting(-1);。您错过了有关 $opts 不存在的严重警告消息 :)
  • 这是因为它隐藏在一个该死的 DIV 中,这会破坏视觉效果,我没有关闭通知...我只是在输出中挖掘并发现我看不到的通知...

标签: php twitter twitter-oauth


【解决方案1】:

好的,作为答案:)

您需要将$options 传递给stream_context_create,而不是未定义的$opts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-26
    • 2023-04-07
    • 2015-06-10
    • 2013-04-29
    • 1970-01-01
    • 2013-06-13
    • 2017-12-18
    • 2018-07-17
    相关资源
    最近更新 更多