質問編集履歴
1
pythonでの実行結果を追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,68 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
### 前提
|
9
|
-
|
9
|
+
pythonで以下のようなソースを書いて、ユーザーの情報を取得する事は成功しています。
|
10
10
|
|
11
|
+
```python
|
12
|
+
import requests
|
13
|
+
import urllib3
|
14
|
+
import setting #Key項目を設定しているファイルだよ。別で管理していない人は、KEY=直書きしてね。
|
15
|
+
from func.func_tweet import getTweetById
|
16
|
+
from func.get_lookup import lookup_main
|
17
|
+
from func.get_users_with_bearer_token import get_users
|
18
|
+
from func.user_tweets import user_tweets
|
11
19
|
|
20
|
+
import json
|
21
|
+
|
22
|
+
http = urllib3.PoolManager()
|
23
|
+
KEY = setting.TWITTER_BEARER_TOKEN
|
24
|
+
|
25
|
+
url = "https://api.twitter.com/2/users/by?usernames=TwitterDev,TwitterAPI&user.fields=description,created_at"
|
26
|
+
|
27
|
+
bearer_token = setting.TWITTER_BEARER_TOKEN
|
28
|
+
|
29
|
+
def connect_to_endpoint(url):
|
30
|
+
response = requests.request("GET", url, auth=bearer_oauth,)
|
31
|
+
print(response.status_code)
|
32
|
+
if response.status_code != 200:
|
33
|
+
raise Exception(
|
34
|
+
"Request returned an error: {} {}".format(
|
35
|
+
response.status_code, response.text
|
36
|
+
)
|
37
|
+
)
|
38
|
+
return response.json()
|
39
|
+
|
40
|
+
def bearer_oauth(r):
|
41
|
+
r.headers["Authorization"] = f"Bearer {bearer_token}"
|
42
|
+
r.headers["User-Agent"] = "v2UserLookupPython"
|
43
|
+
return r
|
44
|
+
|
45
|
+
json_response = connect_to_endpoint(url)
|
46
|
+
print(json.dumps(json_response, indent=4, sort_keys=True))
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
結果は以下のようになりました。
|
51
|
+
|
52
|
+
```json
|
53
|
+
{
|
54
|
+
"data": [
|
55
|
+
{
|
56
|
+
"created_at": "2013-12-14T04:35:55.000Z",
|
57
|
+
"description": "The voice of the #TwitterDev team and your official source for updates,
|
58
|
+
news, and events, related to the #TwitterAPI.",
|
59
|
+
"id": "2244994945",
|
60
|
+
"name": "Twitter Dev",
|
61
|
+
"username": "TwitterDev"
|
62
|
+
},
|
63
|
+
{
|
64
|
+
"created_at": "2007-05-23T06:01:13.000Z",
|
65
|
+
"description": "Tweets about changes and service issues. Follow @TwitterDev\u00a0for more.",
|
66
|
+
"id": "6253282",
|
67
|
+
"name": "Twitter API",
|
68
|
+
"username": "TwitterAPI"
|
69
|
+
}
|
70
|
+
]
|
71
|
+
}
|
72
|
+
```
|
73
|
+
|