Commit 6fb5163
committed
bug symfony#58658 [Twitter][Notifier] Fix post INIT upload (matyo91)
This PR was merged into the 6.4 branch.
Discussion
----------
[Twitter][Notifier] Fix post INIT upload
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues | Fix Twitter Notifier when attaching a media. <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License | MIT
The procedure is described here : [https://developer.x.com/en/docs/x-api/v1/media/upload-media/api-reference/post-media-upload-init](https://developer.x.com/en/docs/x-api/v1/media/upload-media/api-reference/post-media-upload-init). This is tricky as on example Example Request the `media_type` query parameter value is raw encoded : `image/jpeg` or `video/mp4` for exemple.
On Postman, query param value is raw encoded too on the interface, but with PHP CURL generated, it's [Percent encoded](https://developer.x.com/en/docs/authentication/oauth-1-0a/percent-encoding-parameters) or `rawurl encoded` : `video%252Fmp4`. I did test the CURL generation and it was OK. But it's not the case on Twitter Notifier component that produce `[Symfony\Component\Notifier\Exception\TransportException (32)] Could not authenticate you` (so this fix).
<img width="1396" alt="postman" src="https://github.com/user-attachments/assets/172418bf-655f-4e55-87ec-f7a18e322103">
It did several tests, first I think it was the OAuth, but looking at the code on `Symfony\Component\Notifier\Bridge\Twitter\TwitterTransport::request` method and twitter docs, it was fine by comparing the php curl generated by postman and the curl generated by Symfony\Component\HttpClient\CurlHttpClient.
- [https://developer.x.com/en/docs/authentication/oauth-1-0a/authorizing-a-request](https://developer.x.com/en/docs/authentication/oauth-1-0a/authorizing-a-request)
- [https://developer.x.com/en/docs/authentication/oauth-1-0a/creating-a-signature](https://developer.x.com/en/docs/authentication/oauth-1-0a/creating-a-signature)
The difference was on the query (look at media_type param that is raw). This one without the fix produce the Exception.
```bash
[1:27:06][math@mathieus-mbp ~/Sites/darkwood/flow-live] (wave-function-collapse)$ bin/console app:wave-function-collapse -vvv
23:27:44 INFO [http_client] Request: "POST https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=56740&media_type=video/mp4&media_category=tweet_video"
23:27:44 INFO [http_client] Response: "401 https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=56740&media_type=video/mp4&media_category=tweet_video"
In TwitterTransport.php line 247:
[Symfony\Component\Notifier\Exception\TransportException (32)]
Could not authenticate you
Exception trace:
at /Users/math/Sites/darkwood/flow-live/vendor/symfony/twitter-notifier/TwitterTransport.php:247
Symfony\Component\Notifier\Bridge\Twitter\TwitterTransport->processChunk() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/twitter-notifier/TwitterTransport.php:198
Symfony\Component\Notifier\Bridge\Twitter\TwitterTransport->uploadMedia() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/twitter-notifier/TwitterTransport.php:120
Symfony\Component\Notifier\Bridge\Twitter\TwitterTransport->doSend() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/notifier/Transport/AbstractTransport.php:80
Symfony\Component\Notifier\Transport\AbstractTransport->send() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/notifier/Transport/Transports.php:74
Symfony\Component\Notifier\Transport\Transports->send() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/notifier/Chatter.php:46
Symfony\Component\Notifier\Chatter->send() at /Users/math/Sites/darkwood/flow-live/src/Command/WaveFunctionCollapseCommand.php:61
App\Command\WaveFunctionCollapseCommand->execute() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/console/Command/Command.php:279
Symfony\Component\Console\Command\Command->run() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/console/Application.php:1047
Symfony\Component\Console\Application->doRunCommand() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/framework-bundle/Console/Application.php:123
Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/console/Application.php:316
Symfony\Component\Console\Application->doRun() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/framework-bundle/Console/Application.php:77
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/console/Application.php:167
Symfony\Component\Console\Application->run() at /Users/math/Sites/darkwood/flow-live/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:49
Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /Users/math/Sites/darkwood/flow-live/vendor/autoload_runtime.php:29
require_once() at /Users/math/Sites/darkwood/flow-live/bin/console:15
app:wave-function-collapse [--width [WIDTH]] [--height [HEIGHT]] [--dataset [DATASET]]
```
And this one with the fix produce is OK. (look at media_type param that is rawurl encoded)
```bash
[0:11:12][math@mathieus-mbp ~/Sites/darkwood/flow-live] (wave-function-collapse)$ bin/console app:wave-function-collapse -vvv
22:11:18 INFO [http_client] Request: "POST https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=56740&media_type=video%252Fmp4&media_category=tweet_video"
22:11:18 INFO [http_client] Response: "202 https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=56740&media_type=video%252Fmp4&media_category=tweet_video"
22:11:18 INFO [http_client] Request: "POST https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=1849574415295537152&segment_index=0"
22:11:18 INFO [http_client] Response: "204 https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=1849574415295537152&segment_index=0"
22:11:18 INFO [http_client] Request: "POST https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=1849574415295537152"
22:11:19 INFO [http_client] Response: "200 https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=1849574415295537152"
22:11:19 INFO [http_client] Request: "GET https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=1849574415295537152"
22:11:20 INFO [http_client] Response: "200 https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=1849574415295537152"
22:11:20 INFO [http_client] Request: "POST https://api.twitter.com/2/tweets"
22:11:20 INFO [http_client] Response: "201 https://api.twitter.com/2/tweets"
```
To reproduce it :
```php
/** `@var` ChatterInterface $chatter */
$videoFile = new \Symfony\Component\Mime\Part\File('var/cache/dev/wave_function_collapse/wave_function_collapse_6716bd83ad525.mp4');
$message = (new ChatMessage('Daily Flow generation.', (new TwitterOptions())->attachVideo($videoFile)))->transport('twitter');
$chatter->send($message);
```
My tests were only with mp4 file. I didn't test with gif or media_category = 'subtitles' but I assume it's the same logic.
It was a real test done with a tweeter account, so can't really reproduce it without creating your own keys [Twitter notifier docs](https://github.com/symfony/twitter-notifier) with read and write Twitter Access Token + Secret with a twitter account.
Commits
-------
0d87e8e [Twitter][Notifier] Fix post INIT uploadFile tree
2 files changed
+17
-11
lines changed- src/Symfony/Component/Notifier/Bridge/Twitter
- Tests
2 files changed
+17
-11
lines changedLines changed: 9 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
70 | 72 | | |
71 | 73 | | |
72 | 74 | | |
| |||
127 | 129 | | |
128 | 130 | | |
129 | 131 | | |
130 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
131 | 135 | | |
132 | 136 | | |
133 | 137 | | |
134 | 138 | | |
135 | 139 | | |
136 | 140 | | |
137 | 141 | | |
138 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
139 | 145 | | |
140 | 146 | | |
141 | 147 | | |
| |||
Lines changed: 8 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
170 | | - | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
| 174 | + | |
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
178 | | - | |
| 178 | + | |
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
184 | | - | |
185 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
| 188 | + | |
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
| |||
0 commit comments