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.
Related
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.
In the API reference it is stated, that all parameters are optional, except for the project name.
But when I try this:
url = 'https://dialogflow.googleapis.com/v2beta1/projects/'+config["DFproject"]+'/agent/intents'
headers = {'Authorization': "Bearer "+access_token, 'content-type': 'application/json'}
r = requests.post(url, headers=headers)
I get:
{'error': {'code': 400, 'message': "Errors in '' intent: display_name is a required field.", 'status': 'INVALID_ARGUMENT'}}
I thought the display_name comes from the response.
So how am I supposed to set it?
This is the API: Google API
The error is you are using POST in your url. You should be using GET instead for retrievals.
If you want to play around with the API and cant seem to set up the authentication on Postman, feel free to use this : https://developers.google.com/oauthplayground/
and this for reference: https://cloud.google.com/dialogflow/docs/reference/rest/v2-overview
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.
I'm trying to make an app that makes requests to Dynamics CRM Web API from python using urllib2.
So far I can login an user with an Azure application by making a post request to https://login.microsoftonline.com/common/oauth2/authorize
then with the retrieved authorization_code I can get the access_token, refresh_token and others with urllib2
url = 'https://login.microsoftonline.com/common/oauth2/token'
post_fields = {'grant_type': 'authorization_code',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'redirect_uri': REDIRECT_URI,
'resource': 'https://graph.microsoft.com',
'code': code}
request = Request(url, urlencode(post_fields).encode())
resp = urlopen(request).read().decode()
resp = json.loads(resp)
refresh_token = resp['refresh_token']
id_token = resp['id_token']
id_token = jwt.decode(id_token,verify=False)
access_token = resp['access_token']
Then I tried to make another post request by using the access_token but had no luck.
I keep getting:
HTTP Error 401: Unauthorized
Just as a test I make a post directly to .dynamics.com/api/data/v8.1/leads
as follows:
url = 'https://<company_uri>.dynamics.com/api/data/v8.1/leads'
post_fields = {"name": "Sample Account",
"creditonhold": "false",
"address1_latitude": 47.639583,
"description": "This is the description of the sample account",
"revenue": 5000000,
"accountcategorycode": 1
}
request = Request(url, urlencode(post_fields).encode())
request.add_header('Authorization', 'Bearer ' + access_token )
request.add_header("Content-Type", "application/json; charset=utf-8")
request.add_header('OData-MaxVersion','4.0')
request.add_header('OData-Version','4.0')
request.add_header('Accept','application/json')
resp = urlopen(request).read().decode()
But i keep getting the same 401 error code.
I've looked all over msdn documentation but didn't find the way to do this directly without using any library, I just want to use a simple post request.
Since the error code says Unauthorized I think the access_token must be sent in some other way.
Can someone help me on how to correctly use the access_token on Dynamics CRM?
Thanks!
The access token you got back is for the Azure AD Graph API. Not Dynamics CRM.
To call that, you must ask for an access token with resource set to Dynamics CRM API's App ID URI, not https://graph.windows.net.
According to documentation you should set resource to https://<company_uri>.crm.dynamics.com.
So when you are retrieving token:
url = 'https://login.microsoftonline.com/common/oauth2/token'
post_fields = {'grant_type': 'authorization_code',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'redirect_uri': REDIRECT_URI,
'resource': 'https://<company_uri>.crm.dynamics.com',
'code': code}
I wrote simple Python script that suppose to send message from myself to myself via Facebook.
The problems is that I can't obtain page access token.
Permissions are set correctly.
import json
import requests
ACCESS_TOKEN = 'generated this with all possible permissions'
r = requests.get('https://graph.facebook.com/me', params={'access_token': ACCESS_TOKEN})
my_id = r.json()['id']
print(my_id)
r = requests.get('https://graph.facebook.com/me/accounts', params={'access_token': ACCESS_TOKEN})
PAGE_ACCESS_TOKEN = r.json()#####['access_token'] cant be found cos 'data' is empty
print(PAGE_ACCESS_TOKEN)
json = {
'recipient': {'id': my_id},
'message': {'text': 'Hello'}
}
params = {
'access_token': PAGE_ACCESS_TOKEN
}
r = requests.post('https://graph.facebook.com/me/messages', json=json, params=params)
print(r)
It's like I don't have access to data in /user/accounts despite that all permissions are set. I only get my user id.
11330393********
{'data': []}
<Response [400]>
What can be done to fix this?