我已經(jīng)通過創(chuàng)建簽名實(shí)現(xiàn)了一切。我創(chuàng)建了一個(gè)函數(shù)來收集所需的參數(shù)(為了清楚起見,我在此處添加了一些注釋):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 );}我使用這個(gè)函數(shù)來構(gòu)建簽名基字符串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 );}我用這個(gè)函數(shù)來計(jì)算簽名function calculate_signature( $signature_base_string, $signing_key ){ return base64_encode( hash_hmac('sha1', $signature_base_string, $signing_key, true) );}現(xiàn)在構(gòu)建 OAuth 標(biāo)頭。這是一個(gè)函數(shù),它使用上面的輔助函數(shù)(加上其他一些返回所需信息的函數(shù)):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 ); );
- 0 回答
- 0 關(guān)注
- 152 瀏覽
添加回答
舉報(bào)
0/150
提交
取消