Twitter OAUTh and PHP


Twitter OAUTh and PHP

我正在尝试从Twitter API获取请求令牌,以便使用他的Twitter帐户登录用户,但我不断收到401响应"无法验证oauth签名和令牌"。我正在通过PHP执行此操作。我寻找过类似的问题,但显然我是唯一一个疯狂到在没有图书馆的情况下从头开始做这个问题的人。

在他们的 API 文档中,他们谈论"百分比编码"授权标头中发送的值,我使用 urlencode() 函数这样做,不确定它是否正确。

为了计算签名,我使用hash_hmac('SHA1',$signParameters,$hashKey),也不确定它是否是正确的使用。

这是通过 cURL 生成的请求:

POST /oauth/request_token HTTP/1.1
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_callback="http%3A%2F%2Fwww.soytumascota.com%2Ftwitter%2Fuser.php", oauth_consumer_key="MY_APP_KEY", oauth_nonce="0dde25902bde5f3b280f58ea642047cf", oauth_signature_method="HMAC_SHA1", oauth_timestamp="1334697987", oauth_version="1.0", oauth_signature="8313277875f20cd8a8631966a2ba273a5d13aeda"
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

我真的很感激你能提供的任何帮助,谢谢。

编辑:这是我到目前为止编写的代码。

<?php
DEFINE( 'CONSUMER_KEY', 'MY_APP_KEY' );
DEFINE( 'CONSUMER_SECRET', 'MY_APP_SECRET' );
$url = 'https://api.twitter.com/oauth/request_token';
//setting OAuth parameters
$Oauth = Array();
$Oauth['oauth_callback'] = 'http://www.soytumascota.com/twitter/user.php';
$Oauth['oauth_consumer_key'] = CONSUMER_KEY;
$Oauth['oauth_nonce'] = md5( $Oauth['oauth_callback'] . CONSUMER_KEY . time() );
$Oauth['oauth_signature_method'] = 'HMAC_SHA1';
$Oauth['oauth_timestamp'] = (string) time();
$Oauth['oauth_version'] = '1.0';
//signature and authorization header are calculated inside functions
$Oauth['oauth_signature'] = calculateSignature( 'POST', $url, $Oauth );
$authorization = getAuthorizationHeader( $Oauth ); 
ksort( $Oauth );
//setting and sending request using cURL
$curl_session = curl_init( $url );
curl_setopt( $curl_session, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl_session, CURLOPT_POST, true );
curl_setopt( $curl_session, CURLINFO_HEADER_OUT, true );
curl_setopt( $curl_session, CURLOPT_HTTPHEADER, Array( 'Authorization: ' . $authorization ) );
$result = curl_exec( $curl_session );

function getAuthorizationHeader( $parameters )
{
    $authorization = 'OAuth ';
    $j = count( $parameters );
    foreach( $parameters as $key => $val )
    {
        $authorization .= $key . '="' . urlencode( $val ) . '"';
        if( $j-- > 1 )
        {
            $authorization .= ', ';
        }   
    }
    return $authorization;
}
function calculateSignature( $method, $url, $parameters, $accessToken = '' )
{   
    foreach( $parameters as $key => $val )
    {
        $foo = urlencode( $key );
        unset( $parameters[$key] );
        $parameters[$foo] = urlencode( $val );      
    }
    ksort( $parameters );   
    $signBase = '';
    $j = count( $parameters );
    foreach( $parameters as $key => $val )
    {
        $signBase .= "{$key}={$val}";
        if( $j-- > 1 )
        {
            $signBase .= '&';
        }
    }
    $signBase = strtoupper( $method ) . '&' . urlencode( $url ) . '&' . urlencode( $signBase );
    $signKey = urlencode( CONSUMER_SECRET ) . '&' . urlencode( $accessToken );
    $signature = hash_hmac( 'SHA1', $signParameters, $hashKey);
    return $signature;
}

首先,"oauth_signature_method"必须是"HMAC-SHA1"(否则什么都行不通)。

关于"百分比编码",他们在 API 文档中说 https://dev.twitter.com/docs/auth/percent-encoding-parameters 他们需要根据 RFC 3986 对字符串进行编码,因此您应该使用 rawurlencode 而不是 urlencode

要计算签名,他们在这里说 https://dev.twitter.com/docs/auth/creating-signature 您必须使用 HMAC-SHA1 哈希算法并将原始输出转换为 base64,如下所示:

$signature = base64_encode(hash_hmac( 'SHA1', $signBase, $signKey, true));

最后,您可能需要添加

//no content
curl_setopt( $curl_session, CURLOPT_POSTFIELDS, '');
//no verify certs
curl_setopt( $curl_session, CURLOPT_SSL_VERIFYPEER, FALSE);

到 curl 选项以使请求正常工作。