I have been trying to retrieve conversation threads using Tweepy, and although the functionality has been added to the Twitter api (conversation_id is an optional parameter), it has not been added to Tweepy. I was wondering if anyone was familiar enough with Tweepy that they might know a way to achieve this?
Here is my code to get the conversation_id and also to download the conversations. Hopefully it helps people with similar issues. I have only included the required functions not the whole files, so I haven't listed the modules required like requests and base64, but they should be quite obvious.
The code to get the bearer token and create the header I got from here with the Twitter API - how can I get authentication for the engagement endpoint using a bearer token but I reposted below for convenience
# returns a bearer_header to attach to requests to the Twitter api v2 enpoints which are
# not yet supported by tweepy
def get_bearer_header():
uri_token_endpoint = 'https://api.twitter.com/oauth2/token'
key_secret = f"{twitter_creds.consumer_key}:{twitter_creds.consumer_key_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)
bearer_token = auth_resp.json()['access_token']
bearer_header = {
'Accept-Encoding': 'gzip',
'Authorization': 'Bearer {}'.format(bearer_token),
'oauth_consumer_key': twitter_creds.consumer_key
}
return bearer_header
# Returns the conversation_id of a tweet from v2 endpoint using the tweet id
def getConversationId(id):
uri = 'https://api.twitter.com/2/tweets?'
params = {
'ids':id,
'tweet.fields':'conversation_id'
}
bearer_header = get_bearer_header()
resp = requests.get(uri, headers=bearer_header, params=params)
return resp.json()['data'][0]['conversation_id']
# Returns a conversation from the v2 enpoint of type [<original_tweet_text>, <[replies]>]
def getConversation(conversation_id):
uri = 'https://api.twitter.com/2/tweets/search/recent?'
params = {'query': f'conversation_id:{conversation_id}',
'tweet.fields': 'in_reply_to_user_id',
'tweet.fields':'conversation_id'
}
bearer_header = twitter_auth.get_bearer_header()
resp = requests.get(uri, headers=bearer_header, params=params)
return resp.json()
Tweepy does not support v2 of the API yet, although there are plans for this in the coming year.
2022 Edit: Tweepy does support the latest Twitter API at this time.
Related
I would like to know how to scrape data (position, name of the trader, symbol,..)from the Binance leaderboard with Python and Binance API.
Thanks for your answers !
This is my actual code wiche doesn't work.
from binance.client import Client, AsyncClient
api_key = 'xxx'
api_secret = 'xxx'
client = Client(api_key, api_secret)
leaderboard = client.futures_leaderboard()['positions']
I tried the code juste above, but there is no results.
You can use this third party API. Remember to check its documentation.
Here is an example of code to achieve what I think you want:
Getting traders:
import requests
# API URL with its endpoint to use
url = "https://binance-futures-leaderboard1.p.rapidapi.com/v2/searchLeaderboard"
# Parameters to use
querystring = {
"isShared": True, # Set to true if you want to get traders sharing their positions
"limit": 10 # Total traders to get
}
# Headers to use
headers = {
"X-RapidAPI-Key": "YOUR-API-KEY",
"X-RapidAPI-Host": "binance-futures-leaderboard1.p.rapidapi.com"
}
# Get response
response = requests.get(url, headers=headers, params=querystring)
# Print response to JSON
print(response.json())
Getting trader positions:
import requests
# Now we use the endpoint to get the positions shared by a trader
url = "https://binance-futures-leaderboard1.p.rapidapi.com/v2/getTraderPositions"
# Parameters to use
querystring = {
"encryptedUid": "<REQUIRED>", # Trader UUID
"tradeType": "PERPETUAL" # Set to PERPETUAL to get USDⓈ-M positions
}
# Parameters to use
headers = {
"X-RapidAPI-Key": "YOUR-API-KEY",
"X-RapidAPI-Host": "binance-futures-leaderboard1.p.rapidapi.com"
}
# Get response
response = requests.get(url, headers=headers, params=querystring)
# Print response to JSON
print(response.json())
You have to fill in the api key and secret
How to Create API
Creating an API allows you to connect to Binance’s servers via several
programming languages.
I Am getting an error when using the [Gemini API documentation][1] while following the documentation for a Private API invocation.
The json output is:
{'result': 'error', 'reason': 'InvalidSignature', 'message': 'InvalidSignature'}.
My code:
gemini_api_key = getMasterApi()#gets master api key from a json file
gemini_api_secret = getSecretApi().encode()#gets secret api key from a json file
print(gemini_api_secret)
t = datetime.datetime.now()
payload_nonce = str(int(time.mktime(t.timetuple())*1000))
payload = {"request": "/v1/mytrades", "nonce": payload_nonce}
encoded_payload = json.dumps(payload).encode()
b64 = base64.b64encode(encoded_payload)
signature = hmac.new(gemini_api_secret, b64, hashlib.sha384).hexdigest()
request_headers = {
'Content-Type': "text/plain",
'Content-Length': "0",
'X-GEMINI-APIKEY': gemini_api_key,
'X-GEMINI-PAYLOAD': b64,
'X-GEMINI-SIGNATURE': signature,
'Cache-Control': "no-cache"
}
response = requests.post(url, headers=request_headers)
my_trades = response.json()
print(my_trades)
[https://docs.gemini.com/rest-api/#public-api-invocation][1]
I had the same issue and was able to resolve it by creating a new API key with Primary scope (instead of Master scope) and Auditor permissions.
Make sure you're using the right URL. If you made an API key using a sandbox account, you have to change the URL to url = "https://api.sandbox.gemini.com/v1/mytrades".
If you are using the Gemini sandbox, you will need to create your API keys using https://exchange.sandbox.gemini.com/ as opposed to their normal site.
If doing this on your live account and not a sandbox account, when you go to create an API, select 'primary' with 'Fund Management' and 'Trading' permissions. 'Auditor' will not allow you to interact with your funds or place orders.
I had the same issue until I did this.
I am currently working with and API that requires "RFC2045-MIME variant of Base64, except not limited to 76 char/line" this seems to be different from the normal basic auth used in the requests library. Curious if anyone else has come across this and been able to solve it? I imagine I will have to write a function to do this encoding and build the header manually.
Yes, you can create a function as shown below where the headers for the request are created manually.
For this implementation, you will need these variables:
API_PUBLIC_KEY,
API_SECRET_KEY,
host_url,
endpoint (where the API is supposed to hit).
import base64
import requests
def create_request():
auth_header = base64.b64encode(bytes(f'{API_PUBLIC_KEY}:{API_SECRET_KEY}'.encode('ascii'))).decode('utf-8')
headers = {
'Host': host_url,
'Authorization': f'Basic {auth_header}',
'Content-Type': 'application/json'
}
api_endpoint = f'https://{host_url}/{endpoint}'
data = {}
response = requests.request("POST", api_endpoint, headers=headers, data=data)
print(response.text.encode('utf8'))
I have a requirement to access some information through an API. I need to do this on R. I tried doing on Python and it worked just fine but facing 401 error while doing the same operation on R.
I have the API key and also know the query to be performed. I have attached both Python and R code below.
Python:
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
}
query = """
{
boards (ids: 157244624) {
permissions
}
}
"""
response = requests.get('https://XYZwebsite.com/', headers=headers, json={'query': query})
R:
require(httr)
headers = c(
`Content-Type` = 'application/json',
`Authorization` = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
)
data = '{
boards (ids: 157244624) {
permissions
}
}'
res <- GET(url = 'https://XYZwebsite.com/', add_headers(.headers=headers), body = data)
Am i missing out something in the R code? It looks pretty much the same but i am getting a 401 error on R for some weird reason.
In my case api key was expired... really long journey let me to:
amplify update api
walk through all
choose api key
enter new api key name
enter 365 days no further changes
amplify push will not detect changes, so use amplify push --force
maybe it helps
Perhaps try:
r <- GET("https://XYZwebsite.com/",
add_headers(`Content-Type` = "application/json",
`Authorization` = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"),
body = data)
Edit:
With the gist you are working off of:
Authenticate first:
token <- GQL(auth_query)$authenticate$jwtToken
Then you can run your queries by passing the token you get in the response:
GQL(current_person_query, .token = token)
Seems like you are trying to do two things at once.
If you already have the token then it looks like you are passing it incorrectly:
auth_header <- paste("bearer", .token)
res <- POST(.url, body = pbody, encode="json", add_headers(Authorization=auth_header), ...)
As it should be passed as "bearer XXXXXXXXXXXXXXXXX".
Question in short: how to get Facebook API access_token with permission
I want to read the reach for specific ad settings from the Facebook API using Python. In order to do so, I need a facebook access token with extended permissions. I use the following function to get a new access token, but the token I get does not have the proper permission levels. So: how to get an access_token with custom permissions, like you can do manually here?
Python example below (problem is actually language independent):
import requests
import json
from facebookads.adobjects.adaccount import AdAccount
from facebookads.api import FacebookAdsApi
from facebookads.adobjects.adset import AdSet
app_id = 'xxxx'
app_secret = 'xxxx'
account_id = 'xxxx'
def get_fb_token(app_id, app_secret):
payload = {'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': app_secret,
}
file = requests.post('https://graph.facebook.com/oauth/access_token?', params = payload)
string = file.content.decode('utf-8')
json_obj = json.loads(string)
return json_obj['access_token']
access_token = get_fb_token(app_id, app_secret)
account = AdAccount(account_id)
# initiate API
FacebookAdsApi.init(app_id, app_secret, access_token)
# Request reach
targeting_spec = {
'geo_locations': {
'countries': ['nl']
},
}
fb_params = {
'currency': 'EUR',
'optimize_for': AdSet.OptimizationGoal.offsite_conversions,
'targeting_spec': targeting_spec,
}
reach_estimate = account.get_reach_estimate(params=fb_params)
And the error message I get:
(#278) Reading advertisements requires an access token with the extended permission ads_read
Help is highly appreciated!
Try this:
payload = {
'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': app_secret,
'scope': 'ads_read'
}
file = requests.post('https://graph.facebook.com/oauth/access_token?', params = payload)
Also, you need to redirect to the correct endpoint (https://www.facebook.com/v2.xy/dialog/oauth), not POST to it. You cannot get a User token without user interaction.