猿问

无法使用 Twitter V1.1 API 验证用户身份

我已经通过创建签名实现了一切。我创建了一个函数来收集所需的参数(为了清楚起见,我在此处添加了一些注释):

function collect_parameters(){

    global $credentials; // This is an Object with my App's credentials and stuff


    $oAuth = get_user_oauth();  // This returns an object with the the personal user OAuth tokens retrieved from the earlier docs.

    $encoded_collection = array();


    $collection = array(

        'status'                 => rawurlencode( $_GET['tweet'] ),

        'include_entities'       => 'true',

        'oauth_consumer_key'     => $credentials->key,

        'oauth_nonce'            => $credentials->nonce, // md5( str_shuffle( uniqid() . mt_rand(0,9999999999) ) )

        'oauth_signature_method' => 'HMAC-SHA1',

        'oauth_timestamp'        => $credentials->time, // current timestamp

        'oauth_token'            => $oAuth->oauth_token,

        'oauth_version'          => '1.0',

    );


    // Percent encode every key and value that will be signed.

    foreach( $collection as $key => $value ){

        $encoded_collection[rawurlencode($key)] = rawurlencode($value); 

    }


    // Sort the list of parameters alphabetically by encoded key.

    ksort( $encoded_collection );


    return http_build_query( $encoded_collection );

}

我使用这个函数来构建签名基字符串


function create_signature_base_string( $parameter_string, $url = 'https://api.twitter.com/1.1/statuses/update.json', $method = 'POST' ){

    return strtoupper( $method ) .'&'. rawurlencode( $url ) .'&'. rawurlencode( $parameter_string );

}

我用这个函数来计算签名


function calculate_signature( $signature_base_string, $signing_key ){

    return base64_encode( hash_hmac('sha1', $signature_base_string, $signing_key, true) );

}

现在构建 OAuth 标头。这是一个函数,它使用上面的辅助函数(加上其他一些返回所需信息的函数):


function get_oauth_headers(){

    global $credentials;

    $oAuth = get_user_oauth();

    

    $parameters = collect_parameters();

    $signature_base_string = create_signature_base_string( $parameters );

    $signing_key = get_signing_key();

    $signature = calculate_signature( $signature_base_string, $signing_key );


    );



慕斯王
浏览 111回答 0
0回答

冉冉说

您正在执行一些额外的编码。首先,collect_parameters您对数组的键和值进行编码$encoded_collection,然后将其传递给http_build_query进一步对它们进行编码的键和值。您可以完全删除对项目进行编码的循环,而是将它们直接传递给http_build_query. 这里的技巧是它默认为+编码,所以你需要告诉它%使用第四个参数切换到编码:function collect_parameters(){    global $credentials; // This is an Object with my App's credentials and stuff    $oAuth = get_user_oauth();  // This returns an object with the the personal user OAuth tokens retrieved from the earlier docs.    $encoded_collection = array();    $collection = array(        'status'                 => rawurlencode( $_GET['tweet'] ),        'include_entities'       => 'true',        'oauth_consumer_key'     => $credentials->key,        'oauth_nonce'            => $credentials->nonce, // md5( str_shuffle( uniqid() . mt_rand(0,9999999999) ) )        'oauth_signature_method' => 'HMAC-SHA1',        'oauth_timestamp'        => $credentials->time, // current timestamp        'oauth_token'            => $oAuth->oauth_token,        'oauth_version'          => '1.0',    );    // Percent encode every key and value that will be signed.    foreach( $collection as $key => $value ){        $encoded_collection[rawurlencode($key)] = rawurlencode($value);     }    // Sort the list of parameters alphabetically by encoded key.    ksort( $encoded_collection );    return http_build_query( $encoded_collection );}我使用这个函数来构建签名基字符串function create_signature_base_string( $parameter_string, $url = 'https://api.twitter.com/1.1/statuses/update.json', $method = 'POST' ){    return strtoupper( $method ) .'&'. rawurlencode( $url ) .'&'. rawurlencode( $parameter_string );}我用这个函数来计算签名function calculate_signature( $signature_base_string, $signing_key ){    return base64_encode( hash_hmac('sha1', $signature_base_string, $signing_key, true) );}现在构建 OAuth 标头。这是一个函数,它使用上面的辅助函数(加上其他一些返回所需信息的函数):function get_oauth_headers(){    global $credentials;    $oAuth = get_user_oauth();        $parameters = collect_parameters();    $signature_base_string = create_signature_base_string( $parameters );    $signing_key = get_signing_key();    $signature = calculate_signature( $signature_base_string, $signing_key );    $auth_array = array(        'oauth_consumer_key'     => $credentials->key,        'oauth_nonce'            => $credentials->nonce,        'oauth_signature'        => $signature,        'oauth_signature_method' => 'HMAC-SHA1',        'oauth_timestamp'        => $credentials->time,        'oauth_token'            => $oAuth->oauth_token,        'oauth_version'          => '1.0'    );    ksort( $auth_array );    return $auth_array;}现在我已经将所有内容都整理在一个漂亮整
随时随地看视频慕课网APP
我要回答