Premium API endpoint


#1

hi everyone
I am writing using translation software,
English may be a little strange,sorry

Search api
I created an application that works normally
endpoint is
https://api.twitter.com/1.1/search/tweets.json

Today ,i buy Premium Api 30day ,
I saw this webpage
https://developer.twitter.com/en/docs/tweets/search/overview/premium
this webpage has been written

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
If you are using the 30-day endpoint:
https://api.twitter.com/1.1/tweets/search/30day/my_env_name.json
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

my Dev environment label is [SnsTwitter]
I have changed “my_env_name” to “SnsTwitter”.
Edited endpoint is
https://api.twitter.com/1.1/tweets/search/30day/SnsTwitter.json

I thought that it would work normally changing the endpoint, but HTTP Status Codes is 422
Is there anything missing to be HTTP Status Codes 200?

Please let me know f you need more information.
Thanks for your help!


#2

i am reading

how do you make or set YAML file ?


#3

Hi, @csar19783364. I see the cause of the 422 status. Your URL appears to be for the correct endpoint, but it must have a query. Twitter does not know what to send you as a response, because you have not specified, in a query.

You will see, in the docs, that query is required (under the section: Data request parameters)

https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search

Instead of just:
https://api.twitter.com/1.1/tweets/search/30day/my_env_name.json

try:
https://api.twitter.com/1.1/tweets/search/30day/my_env_name.json?query=from:SomeTwitterHandle

The Twitter API response to the first request, without a query, does pinpoint the issue.

HTTP/1.1 422 Unprocessable Entity
content-type: application/json; charset=utf-8
date: Thu, 08 Feb 2018 15:18:36 GMT
UTC; Path=/; Domain=.twitter.com

{"error":{"message":"There were errors processing your request: Missing required parameter:'query', Missing required parameter:'query'","sent":"2018-02-08T15:18:36+00:00","transactionId":"00473ca4002e88c"}}

It is worth receiving the API response body for the error message, as well as the status code returned.


#4

Hi, @lxcichano .
Thank you for replying and help

I understood that the end point is not wrong
https://api.twitter.com/1.1/tweets/search/30day/SnsTwitter.json

I uesd this query ,
https://api.twitter.com/1.1/search/tweets.json?q=abc+&count=500&include_rts=false
I doubt the query,query will be wrong

Probably such this query, I will make it and try it
"https://api.twitter.com/1.1/tweets/search/30day/dev.json" -d ‘{“query”:"(snow)",“fromDate”:“201709270000”, “toDate”:“201709280000”, “maxResults”:10}’ -H "Authorization: Bearer TOKEN

I may come back and ask you again,
Thank you


#5

Hi, @csar19783364. I notice that you used the letter q (q=abc+) instead of the word query (query=abc+) in your query.

In your second query, you do show the whole word query (-d ‘{“query”:"(snow)"). I think that should work.

This example comes from the docs (Note the use of the whole word query):

curl "https://api.twitter.com/1.1/tweets/search/:product/:label.json?query=TwitterDev%20%5C%22search%20api%5C%22&maxResults=500&fromDate=<yyyymmddhhmm>&toDate=<yyyymmddhhmm>" -H "Authorization: Bearer TOKEN"

…On this page:
https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search


#6

Hi, @lxcichano .
Thank you for replying again ,help and advice

First of all, I will do my best until Curl passes.
Then I will think about Python code.

docs POST example is

curl -X POST “https://api.twitter.com/1.1/tweets/search/:product/:label.json” -d
’{“query”:“TwitterDev “search api””,“maxResults”:“500”,“fromDate”:“yyyymmddhhmm”,
“toDate”:“yyyymmddhhmm”}’ -H “Authorization:
Bearer TOKEN”

I changed Below ,From POST example
:product → 30day
:label → SnsTwitter
yyyymmddhhmm(fromDate) → 201802010000
yyyymmddhhmm(toDate) → 201802020000
TOKEN → 999999999999999999-AAAAAAAAAAAAAAAAAAAAAA(My Access Token)
“query”:“TwitterDev “search api”” → “query”:“snow”

The Curl command should be like this

curl -X POST “https://api.twitter.com/1.1/tweets/search/30day/SnsTwitter.json” -d ‘{“query”:“snow”,“maxResults”:“500”,“fromDate”:“201802010000”,
“toDate”:“201802020000”}’ -H “Authorization:
Bearer 999999999999999999-AAAAAAAAAAAAAAAAAAAAAA(My Access Token)”

but ,The response is this
{“error”:{“message”:“Invalid or expired token.”,“sent”:“2018-02-10T15:18:08+00:00”,“transactionId”:“003b61cd0028d019”}}

Can I authenticate with access token only?
Please give me some advice.
Thank you


#7

Hi, @lxcichano .
Thank you for replying again ,help and advice

My program Request succeeded !!

I appreciate it very much.
After all the query in the code was a problem


#8

Congratulations, @csar19783364! That is great news. Could you please share the successful curl command with us so we can learn from it, too?

Thanks,
Lee


#9

Hi, @lxcichano .
Thank you for replying again .

My program Request succeeded,
but I do not know Curl command request yet,sorry


#10

Hello @csar19783364 ,

I guess what @lxcichano asked is what you corrected in this line of code that worked.
"curl -X POST “https://api.twitter.com/1.1/tweets/search/30day/SnsTwitter.json” -d ‘{“query”:“snow”,“maxResults”:“500”,“fromDate”:“201802010000”,
“toDate”:“201802020000”}’ -H “Authorization:
Bearer 999999999999999999-AAAAAAAAAAAAAAAAAAAAAA(My Access Token)”

I’m in a similar fix and your successful code might just help.

I got a lame question though: Who knows how the Bearer Token is gotten? Just can’t find information about it anywhere.

Thanks.


#11

Hello, @ibksolar. Here is the Twitter developers’ bearer token info:

https://developer.twitter.com/en/docs/basics/authentication/api-reference/token.html


#12

@lxcichano

Thanks a bunch for your reply. I am a newbie and I’ve been struggling for over a week to get things done. I’m still trying to grasp couple of things.
I have been on this page earlier but couldn’t make sense of the documentation. The syntax is

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2R … o4OERSZHlPZw==
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

First, what language is this written? Is this Python?

Python has a “pycurl.Curl” library/method, is it the same as the "curl -X POST “https://api.twitter.com/1.1/tweets/search/30day/SnsTwitter.json” " ?


#13

@ibksolar, the example request and response shown would be in HTTP. They are the request and response that you would see in an HTTP inspector, like Fiddler or Wireshark, or a browser’s dev toolbar “network” tab.

You can construct “raw” HTTP posts in tools like Fiddler and Postman. Or, you can construct them in higher level programming languages, like C# or Python.

I’ve coded this in C# on my own, but I now use a library called TweetInvi, which makes it easy to accomplish.

, Lee


#14

Thanks for your prompt reply @lxcichano . Can you please tell me that how can I run following code in python, because this is not python syntax. I’d tried but found errors.

require 'net/http’
require ‘uri’

uri = URI.parse(“https://api.twitter.com/1.1/tweets/search/30day/dev.json”)

headers = {“Authorization”:“Bearer MYREALLYLONGTOKENBASEDONKEYS”, “Content-Type”: “application/json”}

data = {“query”:"{snow OR sleet OR hail OR (freezing rain)) has:images", “fromDate”:“201709270000”,“toDate”:“201709280000”}

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = data

response = http.request(request)

This is the code I’d taken from Twitter Official help.
P.s. I’d done all pre-requisites steps but through this I can’t having extracted tweets.

Can you please help me? or send a online resource?


#15

Hello, @umairhanif00. Your posted code appears to be Ruby. The first detail I notice is that your uri is parsing an SSL endpoint (HTTPS), but your response is not using SSL.

 uri = URI.parse(“https://api.twitter.com/1.1/tweets/search/30day/dev.json”)
response = http.request(request)

This post might help you:

You could also try this converter:


#17

I’d taken help from this link Application-auth. Copied all the stuff and modified the consumer and consumer secret keys.

When I use,

SEARCH_TWEETS_URL = ‘https://api.twitter.com/1.1/search/tweets.json’ # this is standard endpoint
it gives me all the tweets which I am asking

BUT

When I change this to,
SEARCH_TWEETS_URL = '‘https://api.twitter.com/1.1/tweets/search/30day/development.json’ # This is Premium Search endpoint mentioned at Twitter documentation.

It gives me error of invalid request / token.

Can you please tell me where exactly I am doing a mistake?
Thanks :slight_smile: Anxiously waiting for your reply.


#18

Hello, @umairhanif00. In your SEARCH_TWEETS_URL, what looks like the name of the JSON file (/development.json) should match your ‘Dev environment label’. Does it?


#19

Yes my environment label is ‘development’


#20

Are you using the headers and payload variables from that sample code?

It would help to see those, too, but don’t post your actual token. It is hard to troubleshoot code without seeing your code.


#21

headers = {‘Authorization’: ‘Bearer {}’.format(access_token)}
payload = {‘q’: ‘PSL’, ‘count’: 10, ‘result_type’: ‘popular’}

access_token is coming from above code snippet

Sorry for the inconvenience caused, I am from cellphone and can give you this assistance. Will post you the code early in the morning. Can you please tell me through this?