You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 10, 2025. It is now read-only.
API routes sometimes include an argument within them. In such cases the
133
+
positionnal argument should be prefixed with an underscore and reused as
134
+
such in the function's arguments. For instance, to get a user's id when
135
+
knowing only its screen_name, one can use [`https://api.twitter.com/2/users/by/username/:username`](https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by-username-username):
136
+
137
+
```
138
+
res = tw2.users.by.username._username(_username="PythonTwitTools")
139
+
user_id = res["data"]["id"]
140
+
```
141
+
142
+
Similarly, to collect all recent tweets of this account (his "timeline"), using [`https://api.twitter.com/2/users/:id/tweets`](https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-tweets);
Some routes also require an `id` to be given at the end of the route url. In those cases, one can directly use the 'id` argument without prefixing it with an underscore and it will magically be added at the end of the route. For instance to get more metadata on the previous user, thanks to the route [`https://api.twitter.com/2/users/:id`](https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-id)
149
+
150
+
```python
151
+
tw2.users(id=user_id)
152
+
# which is identical to
153
+
tw2.users._id(_id=user_id)
154
+
```
155
+
156
+
Consequently, when calling one of the rare routes raking an argument named "id" but that is not supposed to appear within the route url, it needs to be renamed as `_id` within the function's argument. For instance, to call [`https://api.twitter.com/1.1/collections/show.json`](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/curate-a-collection/api-reference/get-collections-show):
Finally, some routes require to force the HTTP method used to call them (`GET`, `POST`, `DELETE`, etc.). The library uses GET by default and tries to guess most other cases, but it required sometimes to be forced, wghich can be done by using the extra `_method` argument. For instance, the route [`https://api.twitter.com/2/users/:id/muting`](https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/get-users-muting) can be called with GET to collect a list of muted users, and with `POST` to add a user to this list:
165
+
166
+
```python
167
+
# To see the list of all users muted by our previous user:
168
+
tw2.users._id.muting(_id=user_id)
169
+
# And instead to mute this specific user:
170
+
tw2.users._id.muting(_id=user_id, _method="POST")
171
+
```
172
+
40
173
41
174
Examples with Twitter API v2:
42
175
+++++++++++++++++++++++++++++
@@ -295,29 +428,14 @@ attributes.
295
428
`httplib.HTTPHeaders` instance. Use `response.headers.get('h')`
296
429
to retrieve a header.
297
430
298
-
Authentication
299
-
--------------
300
-
301
-
You can authenticate with Twitter in three ways: NoAuth, OAuth, or
302
-
OAuth2 (app-only). Get `help()` on these classes to learn how to use them.
303
-
304
-
OAuth and OAuth2 are probably the most useful.
305
-
306
-
307
-
Working with OAuth
308
-
------------------
309
431
310
-
Visit the Twitter developer page and create a new application:
0 commit comments