Hey @carmenjyuen, I hope you are doing well.

I saw similar questions on this, but they are closed, the reason why I decided to open this one.

So basically I am trying to access the analytics apis but I am getting this error :



{
“errors”: [
{
“code”: “UNAUTHORIZED_ACCESS”,
“message”: “This request is not properly authenticated”
}
],
“request”: {
“params”: {}
}
}```

I am using postam or python to make the request and I am getting the same error still.

Here is how I am making the request with postman. :

Screen Shot 2021-07-05 at 13.09.31
Screen Shot 2021-07-05 at 13.09.37

Neither postman , nor python code which is here is returning a successful response.


import requests
import os
import sys
from tweepy import OAuthHandler
from dotenv import load_dotenv


load_dotenv()


def get_twitter_auth():
    """Setup Twitter authentication.

    Return: tweepy.OAuthHandler object
    """
    try:
        consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
        consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
        access_token = os.getenv('TWITTER_ACCESS_TOKEN')
        access_secret = os.getenv('TWITTER_ACCESS_SECRET')
        assert all([consumer_key, access_secret, access_token, consumer_secret])
    except KeyError:
        sys.stderr.write("TWITTER_*  not found\n")
        sys.exit(1)
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    return auth.apply_auth()


protected_url = "the url"
oauth = get_twitter_auth()
response = requests.get(url=protected_url, auth=oauth)

print(response.content, 10 * "**=|")

FYI :

My application id is : 21264692

When I do :

twurl -t -H ads-api.twitter.com "/8/accounts" I am getting the following response which seems to be a success one:

{"request":{"params":{}},"next_cursor":null,"data":[{"name":"Spiny Trends \uD83E\uDD94","business_name":null,"timezone":"America/Los_Angeles","timezone_switch_at":null,"country_code":null,"id":"18ce5580mcx","created_at":"2020-08-05T06:25:21Z","updated_at":"2020-08-05T06:25:21Z","industry_type":null,"business_id":null,"approval_status":"ACCEPTED","deleted":false}]}

Is there anything I am missing?

Thank you in advance for your time.

EDIT: UPDATE , it seems to work with twurl

Solved,

Looks like I was using the wrong library to make the request with python and authentication.

I end up switching to this and use the authentication as suggested in this answer and manage to make it work.

It was neither an error of the authorization nor authentication.

Full code :

import requests
import os
import sys
import oauth2 as oauth
import json
from dotenv import load_dotenv


load_dotenv()


def get_twitter_auth():
    """Setup Twitter authentication.

    Return: tweepy.OAuthHandler object
    """
    try:
        consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
        consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
        access_token = os.getenv('TWITTER_ACCESS_TOKEN')
        access_secret = os.getenv('TWITTER_ACCESS_SECRET')
        assert all([consumer_key, access_secret, access_token, consumer_secret])
    except KeyError:
        sys.stderr.write("TWITTER_*  not found\n")
        sys.exit(1)
    consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
    access_token = oauth.Token(key=access_token, secret=access_secret)
    client = oauth.Client(consumer, access_token)
    return client


protected_url = "https://ads-api.twitter.com/9/insights/keywords/search?granularity=HOUR&keywords=developers&start_time=2021-07-02T10:00:00Z"
client = get_twitter_auth()
response, data = client.request(protected_url)

print(data, 10 * "==**")
print(response, 10 * "**=|")
2 Likes

Did you got any resolution for this? My developer account have Campaign Analyst permissions to Ads Account, but I still get UNAUTHORIZED_ACCESS issue.