Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not authenticate you in DM #257

Open
vishnu1991 opened this issue Dec 8, 2017 · 4 comments
Open

Could not authenticate you in DM #257

vishnu1991 opened this issue Dec 8, 2017 · 4 comments

Comments

@vishnu1991
Copy link

Error: 32 Could not authenticate you.
To Send Direct messages using "POST direct_messages/events/new (message_create)"
Ref: https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event

$settings = array(
    'oauth_access_token' => "************",
    'oauth_access_token_secret' => "*************",
    'consumer_key' => "************",
    'consumer_secret' => "************"
);

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

/*  Calling TwiiterAPI class */
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();

P.S : Sending Direct message using "POST direct_messages/new" works fine but "POST direct_messages/events/new (message_create)" throws error

@vishnu1991 vishnu1991 changed the title Could not authenticate you Could not authenticate you in DM Dec 8, 2017
@sadlig
Copy link

sadlig commented May 3, 2018

You need to encode your data to post in json like this :
$return_api = $this->twitter->buildOauth($url, $requestMethod)->performRequest(true, [
CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
CURLOPT_POSTFIELDS => json_encode($postfields)
]);

But adding optionnal header generates a bug, I fix it in this PR : #266

@khurshid-alam
Copy link

@sadlig
Same issue. I tried your solution but getting the same error.

@vadim-gutyrchik
Copy link

vadim-gutyrchik commented Mar 3, 2019

was getting "Could not authenticate you" issue too when posting tweets with images; however, posting tweet alone worked, and posting images alone worked too

at the end have found out that problem was with media_id

had such fragment of code:

$post_results_decoded = json_decode($post_results, true);
$media_id = $post_results_decoded['media_id'];

if getting media_id this way, it came as a float number, and got rounded; the error message was related to the issue that twitter cannot find if i have right to post media with this rounded media_id

than i have changed the fragment above to:

$post_results_decoded = json_decode($post_results, true, 512, JSON_BIGINT_AS_STRING);
$media_id = $post_results_decoded['media_id'];

and it works now

hopefully, this helps someone...

@ZarchiMohammad
Copy link

@vishnu1991 Were you able to solve the problem?
I have a similar problem, what should I do?
{"errors":[{"code":215,"message":"Bad Authentication data."}]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants