使用PHP进行Twitter反向身份验证


Twitter reverse authentication with PHP

我正试图在我的api服务器上设置twitter反向认证。我的移动设备将调用此api端点以获取请求令牌,并使用该令牌使用twitter登录,并在手持设备上执行不同的操作。

我正在使用j7mbo/Twitter - API - PHP:最简单的PHP示例检索user_timeline与Twitter API版本1.1

这个库只有一个基本的帖子和获取示例,但我查看了源代码,发现buildOauth可以做twitter所需的一切,即生成签名基字符串和授权头,并使用curl调用端点。

在我的代码中,我设置了我的consumer_key, secret, access_token key和secret,并设置x_auth_mode如下所示:

$tw_settings = array(
        'consumer_key' => $app['config']['twitter_api']['consumer_key'][$culture],
        'consumer_secret' => $app['config']['twitter_api']['consumer_secret'][$culture],
        'oauth_access_token' => $app['config']['twitter_api']['api_access_token'][$culture],
        'oauth_access_token_secret' => $app['config']['twitter_api']['api_access_token_secret'][$culture],
    );
    $postfields = array(
        'x_auth_mode' => 'reverse_auth'
    );
    $twitter = new TwitterAPIExchange($tw_settings);
    $result = $twitter->setPostFields($postfields)
                ->buildOauth($url, $requestMethod)
                ->performRequest();
    return $app->json($result);

但是twitter没有进行身份验证,显示"无法验证oth签名和令牌"。

我终于把它修好了。您可以基于j7mbo/twitter-php-api获得这个git repo。我已经扩展到添加一个方法来获得反向认证令牌从twitter。您可以在服务器上生成这个令牌,并使用您的设备将调用的rest端点公开它,并使用它进行反向身份验证。这样,消费者密钥和消费者秘密就安全地隐藏在服务器上,而不会分发给所有的客户端设备。

Git repo: https://github.com/srinivasmangipudi/twitter-api-php

生成特殊的请求令牌,实例化twitterapiexchanghange对象,然后调用'buildReverseOauth'方法。例句:

$postfields = array(
    'x_auth_mode' => 'reverse_auth'
);
$twitter = new TwitterAPIExchange($tw_settings);
$result = $twitter->setPostfields($postfields)
            ->buildReverseOauth($url, $requestMethod)
            ->performRequest();

这将返回oauth_access_token。