Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

with the Twitter API - how can I get authentication for the engagement endpoint using a bearer token

I'm trying to get engagement data for my company's tweets for a marketing dashboard. I am able to authenticate with Tweepy to get basic Twitter feed data, but the engagement endpoint is giving me trouble. Is it possible that I messing things up by autheticating with Tweepy and then with the bearer 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())

When I call print(engagement_resp.json()) I get the following output:

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

like image 523
Charles Carriere Avatar asked Nov 19 '19 21:11

Charles Carriere


People also ask

How do I authenticate with bearer token?

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.

How do I get my Twitter authentication token?

Login to your Twitter account on developer.twitter.com. Navigate to the Twitter app dashboard and open the Twitter app for which you would like to generate access tokens. Navigate to the "Keys and Tokens" page. Select 'Create' under the "Access token & access token secret" section.

What is access token in Twitter API?

Access Token and Secret: In general, Access Tokens represent the user that you are making the request on behalf of. The ones that you can generate via the developer portal represent the user that owns the App. You will use these to authenticate requests that require OAuth 1.0a User Context.


1 Answers

The answer is in Andy Piper's comment. At the top of this page on Twitter's Developer site it says:

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

On this page that describes the different levels of API access you can find out what's possible in the Standard (free), Premium, and Enterprise API tiers.

like image 179
Tom Bush Avatar answered Sep 30 '22 20:09

Tom Bush