I am trying to call xero Accounts api in python but not working for some reason. It works well for the rest of APIs such as Invoices, reports, and so on, but Accounts.
The following script is my python script i am using to call the api.
get_url = 'https://api.xero.com/api.xro/2.0/Accounts'
response = requests.get(get_url,
headers = {
'Authorization': 'Bearer ' + access_token,
'Xero-tenant-id': xero_tenant_id,
'Accept': 'application/json'
})
json_response = response.json()
print(json_response)
But this is throwing the following error:
{'Type': None, 'Title': 'Unauthorized', 'Status': 401, 'Detail': 'AuthorizationUnsuccessful', 'Instance': '3c1649ef-6eed-4e64-8503-04fc99481db2', 'Extensions': {}}
Can anyone tell me why this is happening? why just Accounts?
Can you share what scopes you're requesting? https://developer.xero.com/documentation/oauth2/scopes
Invoices requires accounting.transactions
Reports requires accounting.reports.read
Accounts requires accounting.settings
Related
I am designing a python GUI, one of its functions is taking a screenshot, uploading it to Imgur and then getting the URL. Though I am having issues understanding the documentation (especially since it says you need to create an account through the API, but not how to do it.). Would anyone be able to explain how exactly to create an account and then upload an image using it?
Note: I am using PIL to get the screenshots, I would prefer you explain it as code written with the requests library or maybe curl (as that isn't too hard to move to python with requests), and I'll be saving only the refresh token in the program, as it would be hardcoded (But the user can change it) and I don't want the user to authenticate.
Thanks in advance.
Edit 1: Also, I will not use imgurpython as it is outdated.
First you have to create normal account on Imgur.
After loging to normal account you can go to https://api.imgur.com/oauth2/addclient to register application.
It needs application name and email. Type of authorization depends on how you will use it.
You should get API keys
Which you can use with API
To get information:
import requests
headers = {
'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
}
#https://i.imgur.com/cvWgXFc.jpg
imageHash = 'cvWgXFc'
r = requests.get(f'https://api.imgur.com/3/image/{imageHash}', headers=headers)
print('status:', r.status_code)
data = r.json()
print(data)
print('size:', data['data']['size'])
Result:
status: 200
{'data': {'id': 'cvWgXFc', 'title': None, 'description': None, 'datetime': 1579572289, 'type': 'image/jpeg', 'animated': False, 'width': 506, 'height': 500, 'size': 89341, 'views': 8087, 'bandwidth': 722500667, 'vote': None, 'favorite': False, 'nsfw': False, 'section': None, 'account_url': None, 'account_id': None, 'is_ad': False, 'in_most_viral': False, 'has_sound': False, 'tags': [], 'ad_type': 0, 'ad_url': '', 'edited': '0', 'in_gallery': False, 'link': 'https://i.imgur.com/cvWgXFc.jpg', 'ad_config': {'safeFlags': ['onsfw_mod_safe', 'share', 'page_load'], 'highRiskFlags': [], 'unsafeFlags': ['not_in_gallery', 'sixth_mod_unsafe'], 'wallUnsafeFlags': [], 'showsAds': False}}, 'success': True, 'status': 200}
size: 89341
To upload:
import requests
import base64
headers = {
'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
}
params = {
'image': base64.b64encode(open('images.png', 'rb').read())
}
r = requests.post(f'https://api.imgur.com/3/image', headers=headers, data=params)
print('status:', r.status_code)
data = r.json()
print(data)
BTW: you can see your registered applications and regenerate API keys (if you forget it) after login on https://imgur.com/account/settings/apps
You will need to use the Imgur API, which you can get from
here:
Once you get the API key from their site, you can start writing some code.
I am trying to upload files to a user OneDrive as that user.
I successfully registered my app and granted the correct permissions.
I am able to upload files to a user drive but only as application and not as the user.
I was able to do the same on google drive using google sdk which allow user impersonation.
I could not find any decent documentation on user impersonation for OneDrive.
import requests
params = {
'grant_type':'client_credentials',
'client_id': 'xxx',
'client_secret': 'yyy',
'resource':'https://graph.microsoft.com'
}
r = requests.post('https://login.microsoftonline.com/{tenant-id}/oauth2/token', data=params, verify=False)
token = r.json()['access_token']
headers = {
'Authorization':token
}
fileHandle = open('somefile.txt', 'rb')
r = requests.put('https://graph.microsoft.com/v1.0/users/{user-id}/drive/items/root:/somefile.txt:/content',
headers=headers, data=fileHandle, verify=False)
fileHandle.close()
this is part of the response I get showing the file was created/modified by my application which make sense:
'createdBy': {
'application': {
'id': 'xxx',
'displayName': 'my_app'
}
},
'lastModifiedBy': {
'application': {
'id': 'xxx',
'displayName': 'my_app'
}
},
my goal is to have my app impersonate my users to create/modify the files.
Is this even supported with onedrive api?
I am doing stripe payment integration using python and use the following data:
import requests
import json
pos = requests.post
url = "https://api.stripe.com/v1/sources"
headers = {'AUTHORIZATION': 'Bearer sk_test_NXht3wZpuYWRIWpMDDqT3RG2'}
data = {
'type': 'alipay',
'owner[email]': 'abc#xyz.com',
'redirect[return_url]': 'https://www.google.com',
'amount': '500',
'currency': 'USD',
'metadata': {
'data': 'data'
}
}
pos(url, data=data, headers=headers).text
json.loads(pos(url, data=data, headers=headers).text)
When give the metadata it gives error '{\n "error": {\n "message": "Invalid hash",\n "param": "metadata",\n "type": "invalid_request_error"\n }\n}\n'
but according to stripe documentation metadata can be used( https://stripe.com/docs/api/curl#create_source-metadata)
Can anyone tell the solution why it gives that error.
This will solve the problem.
import requests
import json
pos = requests.post
url = "https://api.stripe.com/v1/sources"
headers = {'AUTHORIZATION': 'Bearer sk_test_NXht3wZpuYWRIWpMDDqT3RG2'}
data = {
'type': 'alipay',
'owner[email]': 'abc#xyz.com',
'redirect[return_url]': 'https://www.google.com',
'amount': '500',
'currency': 'USD',
'metadata[data]': 'data'
}
pos(url, data=data, headers=headers).text
json.loads(pos(url, data=data, headers=headers).text)
Stripe does not support JSON payloads for the parameters. Instead, they require application/x-www-form-urlencoded.
At the moment, you are sending metadata as a hash and you are not encoding it properly so Stripe is rejecting it.
The best solution here is to avoid doing this yourself and instead rely on Stripe's official Python library that you can find here: https://github.com/stripe/stripe-python
I'm having difficulties getting Microsoft Graph to return two test Contact Folders that I have set up named Test and Test 2.
When I use v1.0:
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json'}
url = 'https://graph.microsoft.com/v1.0/me/contactFolders'
response = requests.get(url,headers=headers)
response_data = response.json()
print(response_data)
I get a blank value in the response:
{
'#odata.context': "https://graph.microsoft.com/v1.0/$metadata#users('jacobdansey%40hotmail.com')/contactFolders",
'value': []
}
When I use the Beta, I get this which at least returns something but not what I'm looking for:
{
'#odata.context': "https://graph.microsoft.com/beta/$metadata#users('jacobdansey%40hotmail.com')/contactFolders",
'value': [{
'id': '*ID*',
'parentFolderId': '*ParentID*',
'displayName': 'Contacts',
'wellKnownName': 'contacts'
}, {
'id': '*ID*',
'parentFolderId': '*ParentID*',
'displayName': 'Skype Contacts',
'wellKnownName': 'skypecontacts'
}]
}
I know I am connecting properly because when I ask for just contacts from https://graph.microsoft.com/v1.0/me/contacts, it returns the correct answer.
Any help would be greatly appreciated, thanks!
EDIT: Is there a difference between contact folders and contact lists?
i am assuming you want access to folder to get the contacts, if thats the case then you can directly get the contacts of that folder changing the get url
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list_contacts
I am having trouble with the Jawbone API. When I step through the oauth proess using https://github.com/karthikbgl/python-jawbone-up.git I can successfully receive an authorization code and then get a token.
def access_token(self, code, grant_type='authorization_code'):
'''
Get the access code for a user with a auth code.
'''
params = {
'code' : code,
'client_id' : self.client_id,
'client_secret' : self.client_secret,
'grant_type' : grant_type
}
context = {
'base_url': self.base_url,
'params' : urllib.urlencode(params)
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'X-Target-URI': 'https://jawbone.com',
'host': 'jawbone.com'
}
token_url = '{base_url}auth/oauth2/token/?{params}'.format(**context)
res = requests.get(token_url, headers=headers)
return res.json()
However the token I receive is always the same, and when I use it to call the API, I receive the error :
{"meta": {"code": 401, "error_detail": "You must be logged in to perform that action", "error_type": "authentication_error", "message": "Unauthorized"}, "data": {}}
Additionally, if I use the module called access_jawbone with this code:
params = urllib.urlencode({
'email': username,
'pwd': password,
'service': 'nudge'
})
tokenresponse = urllib2.urlopen(url, params)
I get a valid token that I can access the API with.
Does anyone know why the oauth token provided is not working
The below question seems to address the same problem, but I do not understand the answer or how to resolve my problem.
Jawbone UP API oAuth and Access Tokens
I had the same issue. I think that this answer is what is happening:
https://stackoverflow.com/a/32984287/1732987
To get around it, I had to log out of all Jawbone stuff in by browser, remove the access token from my new application database, and start the sign in process from a clean slate.