abraham / twitteroauth

The most popular PHP library for use with the Twitter OAuth REST API.
https://twitteroauth.com
MIT License
4.3k stars 1.71k forks source link

Upload Media always null #1221

Closed lucaschevalierr closed 8 months ago

lucaschevalierr commented 8 months ago

What are you attempting to do I'm trying to upload a photo, but my result is always null...

I was inspired by what was done here: https://stackoverflow.com/questions/76504748/twitter-api-v2-create-tweet-with-media-php/76505243#76505243

Here's my code:

$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$connection->setApiVersion(1.1);
$media = $connection->upload('media/upload', [
    'media' => 'imgs/test.jpeg'
]);

dd($media);

Versions:

Additional context I also tried not specifying the API version, adding parameters like media type, but nothing worked.

ProdigyView commented 8 months ago

Same here. My code

<?php

require "vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);

$connection->setApiVersion(1.1);
$connection->setTimeouts(10, 15);

$media1 = $connection->upload('media/upload', ['media' => '/code/test.mp4','media_type' => 'video/mp4', 'media_category' => 'tweet_video']);

print_r($media1);
print_r("Why are you null?");

$connection->setApiVersion(2);
$parameters = [
    'text' => 'Meow Meow Meow',
    'media' => ['media_ids' => [$media1->media_id_string]]
];
$result = $connection->post('tweets', $parameters);
ProdigyView commented 8 months ago

@lucaschevalierr Solved it! Use the chunk upload.

$media1 = $connection->upload('media/upload', ['media' => '/code/test.mp4','media_type' => 'video/mp4', 'media_category' => 'tweet_video'], ['chunkedUpload' => true]);

lucaschevalierr commented 8 months ago

@ProdigyView It doesn't work for me... I try to do this :

$media = $connection->upload(
            'media/upload',
            [
                'media' => 'imgs/test.jpeg',
                'media_type' => 'image/jpeg',
                'media_category' => 'tweet_image'
            ],
            ['chunkedUpload' => true]
        );

And i have this error : property_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

ProdigyView commented 8 months ago

I would the the absolute page to your image:

'media' => 'imgs/test.jpeg',

lucaschevalierr commented 8 months ago

I would the the absolute page to your image:

'media' => 'imgs/test.jpeg',

I already try, but it doesn't work either

lucaschevalierr commented 8 months ago

Anyone have an idea?

abraham commented 8 months ago

And i have this error : property_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

What's the rest of the error? What method is getting an invalid value?

lucaschevalierr commented 8 months ago

And i have this error : property_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

What's the rest of the error? What method is getting an invalid value?

Here is the rest of the error : image

abraham commented 8 months ago

If you are not on the latest version of TwitterOAuth you would need to use the old method of setting chunkedUpload

$media = $connection->upload(
            'media/upload',
            [
                'media' => 'imgs/test.jpeg',
                'media_type' => 'image/jpeg',
                'media_category' => 'tweet_image'
            ],
            true
        );
lucaschevalierr commented 8 months ago

If you are not on the latest version of TwitterOAuth you would need to use the old method of setting chunkedUpload

$media = $connection->upload(
            'media/upload',
            [
                'media' => 'imgs/test.jpeg',
                'media_type' => 'image/jpeg',
                'media_category' => 'tweet_image'
            ],
            true
        );

So I ran some tests:

PS: I think I am using the latest version of TwitterOAuth because when I try your code, I get this error: "Expected type 'array'. Found 'true'.” Correction (3)

PS2: Thank you very much for the time you spend helping me.

abraham commented 8 months ago

I think you have an authentication issue. To use that API you need client credentials and user credentials. You would want to use the the values with the following names. You'd also need to make sure that the user has granted write permissions for that token and if the user had previously granted read only permissions, the app needs to be revoked and authorized again.

$connection = new TwitterOAuth('API Key', 'API Secret', 'Access Token', 'Access Token Secret');
lucaschevalierr commented 8 months ago

I think you have an authentication issue. To use that API you need client credentials and user credentials. You would want to use the the values with the following names. You'd also need to make sure that the user has granted write permissions for that token and if the user had previously granted read only permissions, the app needs to be revoked and authorized again.

$connection = new TwitterOAuth('API Key', 'API Secret', 'Access Token', 'Access Token Secret');

OMG, the last time I created the token I didn't have the right permissions, I didn't see that it was here.

image

Thank you and really sorry...