J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 800 forks source link

https://api.twitter.com/1.1/direct_messages/events/new.json #268

Closed gentjana closed 6 years ago

gentjana commented 6 years ago

I'm using the below code for replying to a direct message, but returns me this error: "{"errors":[{"code":215,"message":"Bad Authentication data."}]}". Please help is important to fix it ASAP. Thanks.

$settings = array( 'oauth_access_token' => $channel->accessToken, 'oauth_access_token_secret' => $channel->accessTokenSecret, 'consumer_key' => $twApp->apiKey, 'consumer_secret' => $twApp->apiSecret );

$twitter = new TwitterAPIExchange($settings);

$url = 'https://api.twitter.com/1.1/direct_messages/events/new.json';

$params = json_encode(array( 'event' => array( 'type' => 'message_create', 'message_create' => array( 'target' => array( "recipient_id" => $this->fromUserId), 'message_data' => array( 'text' => $text ) ) ) ) );

    $requestMethod = 'POST';

    echo $twitter->buildOauth($url, $requestMethod)
        ->performRequest(true,
            array(CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ), CURLOPT_POSTFIELDS => $params)
        );
jsmoral commented 6 years ago

Hi, I'm having the same issue. I have updated the library but still get the code:215 error with the same code as @gentjana

pedroip commented 6 years ago

Yo lo solucione modificando la clase así:

a la clase TwitterAPIExchange , añade la propiedad public $appjson;

en public function __construct(array $settings) añade la inicialización. $this->appjson=false;

en public function performRequest($return = true, $curlOptions = array())

remplaza $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
por if ($this->appjson) { $header = array('Content-Type: application/json',$this->buildAuthorizationHeader($this->oauth), 'Expect:');
} else { $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
}

para finalizar usa:

..... $twitter = new TwitterAPIExchange($settings); ...... ...... $twitter->appjson=true; $twitter->buildOauth($url, $requestMethod) ->performRequest(true, array( CURLOPT_POSTFIELDS => $params) );

jsmoral commented 6 years ago

@pedroip Gracias Pedro!. Funciona. Saludos

J7mbo commented 6 years ago

For the record, you need to make sure that your tokens have both read and write permissions, and direct message permissions, which are a separate permission you have to 'tick the box' for.

As this is an old issue, I'm closing it, but if you still feel that something needs to be addressed, please feel free to open a new issue and we'll solve it together! :)

thomasesmith commented 6 years ago

Here's @pedroip's solution translated in to English. It worked for me, so I thought I would just make it a little clearer for people who don't speak Spanish...

This can be solved by modifying the class like this:

In the TwitterAPIExchange class add the property: public $appjson;

Add the following as the final line in the __construct method of the class: $this->appjson = false;

In the performRequest method of the class, comment out the line, $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');

and beneath it, put the following:

if ($this->appjson)
{
    $header = array('Content-Type: application/json', $this->buildAuthorizationHeader($this->oauth), 'Expect:');
} else {
    $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
}

Then, you can use the DM methods like this:

$postfields = array (
                        'event' => 
                        array(
                            'type' => 'message_create',
                            'message_create' => 
                            array(
                                'target' => array('recipient_id' => '0000000'),
                                'message_data' => array('text' => 'Hello World!')
                            )
                        )
                    );
$url = 'https://api.twitter.com/1.1/direct_messages/events/new.json';
$requestMethod = 'POST';

$twitter = new TwitterAPIExchange($settings);
$twitter->appjson = true;
$twitter->buildOauth($url, $requestMethod)->performRequest(true, 
    [
        CURLOPT_POSTFIELDS => json_encode($postfields)
    ]
);

Thanks, @pedroip.

ijazraja commented 5 years ago

// I am sending a direct message using Twitter OAuth library but in $result it is sending null

$data = [ "event" => [ "type" => "message_create", "message_create" => [ "target" => [ "recipient_id" => $account_id ], "message_data" => [ "text" => $msg ] ] ] ];

   $result =  $connection->post('direct_messages/events/new',array($data), true);
ZarchiMohammad commented 3 years ago

@thomasesmith Thanks very much for your solution 😘🌺

JLuc commented 3 years ago

Could the library be fixed in case there is a solution ?

ZarchiMohammad commented 3 years ago

@JLuc Yes, and I put it at this address: click to view