PHP7.2を使ってます。
https://qiita.com/tetra-sol/items/32b144b5fff432a912fa#%E6%B3%A8%E9%87%88%E5%8F%82%E8%80%83%E3%82%B5%E3%82%A4%E3%83%88 を参考に
PHPでDMを送るプログラムを書いています。
しかしながら、レスポンスが以下の通りとなりうまくいきません。
PHP
1array( 2 'errors' => array( 3 (int) 0 => array( 4 'code' => (int) 32, 5 'message' => 'Could not authenticate you.' 6 ) 7 ) 8)
※XXX,YYY,ZZZ,OOOの部分は適切なものが入っており、同じキーでTweetはできていますし、
DMの権限は取得できています。
何が原因と思いますか?
($message_dataの中身は適切に設定できていることを確認しています。)
PHP
1private function dmcreater($uid,$message_data){ 2 $api_key = 'XXX'; 3 $api_secret = 'YYY'; 4 $access_token = 'ZZZ'; 5 $access_token_secret = 'OOO'; 6 $signature_key = rawurlencode( $api_secret )."&".rawurlencode( $access_token_secret ); 7 $paramData = array( 8 "oauth_token" => rawurlencode( $access_token ), 9 "oauth_consumer_key" => rawurlencode( $api_key ), 10 "oauth_signature_method" => rawurlencode( "HMAC-SHA1" ), 11 "oauth_timestamp" => time(), 12 "oauth_nonce" => microtime(), 13 "oauth_version" => rawurlencode( "1.0" ) 14 ); 15 $sig_param = rawurlencode('POST')."&". 16 rawurlencode('https://api.twitter.com/1.1/direct_messages/events/new.json')."&". 17 rawurlencode( http_build_query( $paramData , "", "&" ) ); 18 ksort( $paramData ); 19$signature = hash_hmac( "sha1", $sig_param, $signature_key, TRUE ); 20$signature = base64_encode( $signature ); 21$paramData['oauth_signature'] = $signature; 22$httpHeader = array( 23 'Authorization: OAuth '.http_build_query( $paramData, "", "," ), 24 'content-type: application/json' 25 ); 26 27 $postData = array( 28 "event" => array( 29 "type" => "message_create", 30 "message_create" => array( 31 "target" => array( 32 "recipient_id" => "$uid" //送信先ID 33 ), 34 "message_data" => $message_data 35 ) 36 ) 37 ); 38 debug($httpHeader); 39 $ch = curl_init(); 40 curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader); 41 curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/1.1/direct_messages/events/new.json'); 42 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, rawurlencode('POST')); 43 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 44 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $postData )); 45 46 $json = curl_exec($ch); 47 curl_close($ch); 48 $response = json_decode( $json, true ); 49 debug($response); 50 return $response; 51}