/stats endpoints fail with User OAuth tokens. Other Ads API requests work fine


#1

I get a “401 Authorization Required” error whenever I make a request to a /stats API endpoint. I’m using 3-legged OAuth to retrieve tokens and sign users in. These tokens work fine for all other Ads API and Rest API requests.

According to this page, I should be able to access ads analytics:
https://dev.twitter.com/ads/campaigns/obtaining-ads-account-access

Is there something else I should be doing (requesting additional permissions, etc)?


#2

Hi @amirmanji

  1. Can you post the full and raw HTTP request and response for one of the API calls that’s failing for you?
  2. Do the calls that succeed use the same access token as the calls that fail?

#3

I’m using the same access token for the API calls that are failing as the ones that are succeeding.

Successful request:

GET /0/accounts/cj3ur/campaigns HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: OAuth gem v0.4.7
Authorization: OAuth oauth_consumer_key=\"po7Xfr5dz81Ularr9dHcSa2fD\", oauth_nonce=\"56PJLibx1moVehM3bw8pTiqJiRcZoaUzR1erP0f7nA\", oauth_signature=\"7bS2qE1DBcQJY2DjLsgiP36NKk4%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1424390850\", oauth_token=\"2349250652-oR3k7QRaiylBkcHRR8EizSqTdTPlQUwlwyLQKSU\", oauth_version=\"1.0\"
Connection: close
Host: ads-api.twitter.com

Failing request:

GET /0/stats/accounts/cj3ur?start_time=2015-02-13T00:00:00+00:00&end_time=2015-02-14T00:00:00+00:00 HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: OAuth gem v0.4.7
Authorization: OAuth oauth_consumer_key=\"po7Xfr5dz81Ularr9dHcSa2fD\", oauth_nonce=\"CRyC6S2NBdJkfLfwlUai7r38QpeIyBDY7KZUtlxq8\", oauth_signature=\"tym51Y7hN2z0Hs38c9NldsOH15w%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1424390975\", oauth_token=\"2349250652-oR3k7QRaiylBkcHRR8EizSqTdTPlQUwlwyLQKSU\", oauth_version=\"1.0\"
Connection: close
Host: ads-api.twitter.com

Failing response body:

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

Failing response headers:

HTTP/1.1 401 Authorization Required
connection: close
content-disposition: attachment; filename=json.json
content-encoding: gzip
content-length: 139
content-type: application/json;charset=utf-8
date: Fri, 20 Feb 2015 00:18:38 UTC
server: tsa_b
set-cookie: guest_id=v1%3A142439151803636126; Domain=.twitter.com; Path=/; Expires=Sun, 19-Feb-2017 00:18:38 UTC
strict-transport-security: max-age=631138519
x-connection-hash: 576bf43618531957a8178da24520f637
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 87
x-runtime: 3.5E-5
x-transaction: 0840d51ed2b568dc
x-xss-protection: 1; mode=block

#4

@amirmanji I just tried to reproduce and the request that failed for you returned successfully for me. That leads me to think this is an Oauth issue. Have you tried using twurl?


#5

This turned out to be a problem with parameter formatting. I tried out Twurl and my requests went through. The difference was that Twurl was escaping my query parameters, whereas my previous attempts weren’t:

Original, failing request URL:

GET /0/stats/accounts/cj3ur?start_time=2015-02-13T00:00:00+00:00&end_time=2015-02-14T00:00:00+00:00 HTTP/1.1

Equivalent Twurl request URL:

GET /0/stats/accounts/cj3ur?start_time=2015-02-13T00%3A00%3A00%2B00%3A00&end_time=2015-02-14T00%3A00%3A00%2B00%3A00 HTTP/1.1

I’m using the same Rubygem as Twurl for my requests (oauth 0.4.7), so I mimicked the parameter escaping logic from Twurl found here:

in my own requests, which fixed the problem.