0

This is my code

// Replace these with your actual keys and tokens
    $consumer_key = 'consumer_key';
    $consumer_secret = 'consumer_secret';
    $access_token = 'access_token';
    $access_token_secret = 'access_token_secret';

    // Your tweet content
    $tweet = "Hello, this is my tweet from localhost! #PHP #TwitterAPI";

    // Create the OAuth 1.0a header
    $oauth_nonce = md5(uniqid(rand(), true));
    $oauth_timestamp = time();

    $oauth_signature_key = rawurlencode($consumer_secret) . '&' . rawurlencode($access_token_secret);

    $oauth_params = [
        'oauth_consumer_key' => $consumer_key,
        'oauth_nonce' => $oauth_nonce,
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_timestamp' => $oauth_timestamp,
        'oauth_token' => $access_token,
        'oauth_version' => '1.0'
    ];

    $base_string = 'POST&' . rawurlencode('https://api.twitter.com/1.1/statuses/update.json') . '&' . rawurlencode(http_build_query($oauth_params) . '&' . rawurlencode('status=' . rawurlencode($tweet)));


    $oauth_signature = base64_encode(hash_hmac('sha1', $base_string, $oauth_signature_key, true));

    $oauth_params['oauth_signature'] = $oauth_signature;

    $oauth_header = 'OAuth ' . urldecode(http_build_query($oauth_params, '', ', '));

    // Prepare the cURL request
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/update.json');
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded", "Authorization: $oauth_header"]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ['status' => $tweet]);

    // Send the tweet
    $result = curl_exec($ch);

    // Check for cURL errors
    if (curl_errno($ch)) {
        // echo "cURL Error: " . curl_error($ch);
        $log_message =  "cURL Error: " . curl_error($ch) . PHP_EOL;
        file_put_contents(plugin_dir_path(__FILE__) . 'publish-log.txt', $log_message, FILE_APPEND);
        // Additional error handling if needed
    } else {
        // Check for HTTP status code
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 200) {
            echo "Tweet sent successfully!";
        } else {
            // echo "Error sending tweet: " . $result;
            $log_message =  "Error sending tweet: " . $result;
            file_put_contents(plugin_dir_path(__FILE__) . 'publish-log.txt', $log_message, FILE_APPEND);
        }
    }

    // Close cURL resource
    curl_close($ch);

But getting this in error log,

Error sending tweet: {"errors":[{"code":32,"message":"Could not authenticate you."}]}

I get tokens and secrets for twitter developer account

enter image description here

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • @CBroe yup and I added in last screenshot where I got them – vimuth Jul 31 '23 at 12:02
  • You got to double-check how you are calculating the signature and the header then, I'd say. (I can't even really tell from their documentation, what the header is supposed to look like - are the values supposed to go in double quotes, or is that only "syntax" they use in the documentation to signal "a value needs to be inserted here" ...)) Or use an existing library/SDK, perhaps, instead of implementing it yourself. – CBroe Jul 31 '23 at 12:08
  • Looking at the [documentation](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update) for that endpoint, they are creating the oauth-data differently than you, among other things. You 're adding them in with `oauth_xxx=foo&oauth_xxx=bar&...` (like a query string) while they are using: `oauth_xxx="foo",oauth_xxx="bar",....` (with comma instead). You're also encoding the `=` signs, which you shouldn't do. Only encode the values. – M. Eriksson Jul 31 '23 at 12:14

0 Answers0