gpt4 book ai didi

python - 使用 Twitter API - 如何使用不记名 token 获得参与端点的身份验证

转载 作者:行者123 更新时间:2023-12-04 21:28:41 27 4
gpt4 key购买 nike

我正在尝试获取我公司推文的参与度数据以用于营销仪表板。我能够通过 Tweepy 进行身份验证以获取基本的 Twitter 提要数据,但参与端点给我带来了麻烦。我是否有可能通过使用 Tweepy 然后使用不记名 token 进行身份验证来搞砸?

import tweepy
import requests
import json
import base64
import urllib.parse

consumer_key = <>
consumer_secret = <>
access_token = <>
access_token_secret = <>


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

print(api.me().name)


def get_tweet_ids():
scandy_tweets = api.user_timeline('TwitterHandle', count=5)
tweet_id_list = []
for twit in scandy_tweets:
json_str = json.loads(json.dumps(twit._json))
tweet_id_list.append(json_str['id'])
return tweet_id_list


def get_bearer_token():
uri_token_endpoint = 'https://api.twitter.com/oauth2/token'
key_secret = f"{consumer_key}:{consumer_secret}".encode('ascii')
b64_encoded_key = base64.b64encode(key_secret)
b64_encoded_key = b64_encoded_key.decode('ascii')

auth_headers = {
'Authorization': 'Basic {}'.format(b64_encoded_key),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}

auth_data = {
'grant_type': 'client_credentials'
}

auth_resp = requests.post(uri_token_endpoint, headers=auth_headers, data=auth_data)
print(auth_resp.status_code)
bearer_token = auth_resp.json()['access_token']
return bearer_token



bearer_token = get_bearer_token()

bearer_header = {
'Accept-Encoding': 'gzip',
'Authorization': 'Bearer {}'.format(bearer_token),
'oauth_consumer_key': consumer_key
}

recent_tweets = get_tweet_ids()

engage_data = {
'tweet_id_list': recent_tweets,
'engagement_types': ['impressions', 'engagements', 'favorites'],
'groupings': {'grouping name': {'group_by': ['tweet.id', 'engagement.type']}}
}

uri_28hr_endpoint = 'https://data-api.twitter.com/insights/engagement/28hr'
engagement_resp = requests.post(uri_28hr_endpoint, headers=bearer_header, data=engage_data)


print(engagement_resp.status_code)
print(engagement_resp.json())

当我打电话时 print(engagement_resp.json())我得到以下输出:

403 {'errors': ['Your Application ID is not authorized.']}

最佳答案

答案在 Andy Piper 的评论中。置顶 this page on Twitter's Developer site它说:

This is an enterprise API available within our managed access levels only. To use this API, you must first set up an account with our enterprise sales team



this page that describes the different levels of API access您可以了解标准(免费)、高级和企业 API 层的可能性。

关于python - 使用 Twitter API - 如何使用不记名 token 获得参与端点的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58943052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com