POST call to `stats/jobs/accounts/:account_id` is unauthorized

restapi
analytics

#1

Below is the code I am using to make this call. Prior to the call in the title, I successfully make GET requests to other endpoints in the Ads API, however when I make this call:

const Twitter = require('twitter')
const adsTwitterConfig = {
  consumer_key: config.TWITTER_ADS_API_KEY,
  consumer_secret: config.TWITTER_ADS_API_SECRET_KEY,
  access_token_key: config.TWITTER_ADS_ACCESS_TOKEN,
  access_token_secret: config.TWITTER_ADS_ACCESS_TOKEN_SECRET,
}
let adsClient = new Twitter(adsTwitterConfig)

let path = `stats/jobs/accounts/${ad_account_id}?start_time=${start_time}&end_time=${end_time}&entity=LINE_ITEM&entity_ids=${entity_id}&granularity=DAY&metric_groups=BILLING,ENGAGEMENT,MEDIA,VIDEO,WEB_CONVERSION&placement=${placement}`

let res = await adsClient.post('https://ads-api.twitter.com/4/' + path, {})

I receive the following result:

[ { code: 'UNAUTHORIZED_ACCESS', message: 'This request is not properly authenticated' } ]

My application has Read and Write permissions, and keys+tokens were configured properly.


#3

Hi @wavome ,

Hmm, could you please provide us your actual “path” value so that we can test? I just did my quick test with your sample code but it worked.

Best,
Shohei


#4

Hi @jrsyo, thanks for getting back to me!

The full request URL would be

https://ads-api.twitter.com/4/stats/jobs/accounts/c3l9t?start_time=2018-05-13T00:00:00-04:00&end_time=2018-06-02T00:00:00-04:00&entity=LINE_ITEM&entity_ids=bnw8s&granularity=DAY&metric_groups=BILLING,ENGAGEMENT,MEDIA,VIDEO,WEB_CONVERSION&placement=ALL_ON_TWITTER

#5

Hi @wavome ,

I think that’s just a URI encoding issue as your URI include : that need to be encoded to %3A. Maybe you can do something like this:

const Twitter = require('twitter')
// added "querystring" module
const qs = require("querystring");

const adsTwitterConfig = {
  consumer_key: config.TWITTER_ADS_API_KEY,
  consumer_secret: config.TWITTER_ADS_API_SECRET_KEY,
  access_token_key: config.TWITTER_ADS_ACCESS_TOKEN,
  access_token_secret: config.TWITTER_ADS_ACCESS_TOKEN_SECRET,
}
let adsClient = new Twitter(adsTwitterConfig)

const params = {
  start_time: '2018-05-13T00:00:00-04:00',
  end_time: '2018-06-02T00:00:00-04:00',
  entity: 'LINE_ITEM',
  entity_ids: 'bnw8s',
  granularity: 'DAY',
  metric_groups: 'BILLING,ENGAGEMENT,MEDIA,VIDEO,WEB_CONVERSION',
  placement: 'ALL_ON_TWITTER'
}

let res = await adsClient.post('https://ads-api.twitter.com/4/stats/jobs/accounts/c3l9t?' + qs.stringify(params), {})

Best,
Shohei


#6

@jrsyo

aaaaaahhhh I guess I assumed the client would take care of that. THANK YOU! :smiley: