How to get data from amoCRM through API? - python

I am trying to get data through API from amoCRM system but I am having some problems and I am really tired.
The url: https://alamiri.amocrm.ru/api/v4/contacts
This API should return contacts of users that contain (first name, email, number, ...).
When I copy and paste the above link in the browser, I get the data and everything works fine but when I try to get that data in Python it returns the following:
{'detail': 'Invalid user name or password',
'status': 401,
'title': 'Unauthorized',
'type': 'https://httpstatus.es/401'
}
my code in python (Django framework):
import requests
import pprint
def get_contacts():
url = "https://alamiri.amocrm.com/api/v4/contacts"
api_key = "def50200c1f82415f4625f96e402d967b880e33938b94d6ad8edd6c305cda8a3fff80990871fba6aac89391440c6330a26768410a52b0e332b2c6a1b2e49cbe912132194d4bb8c4a15bb304c14f649f1149826fc0eeaf1311f23feeea1118cba72af36e76c22f30fa2fb81a4f0678cde451492504ba56eff9eb7af876e49ad721105a9b8bcc94400bf21e9c9f5de43a17daa439d5af64a9ae8e54bf8c7005f4f01515cfe8756a0ddd209fd91c4ca15bc336ef89523876dd39f6472f190f82b1ecd4d9d0cfccc6941c1ffb64d79e2e6dbec1b12331154a7b343ac10abfc05f432d09da878d41a37cdacc50b1f18419594e669854efa108c17384143a71a4d6739bad60cdc704728e2c0dce82ff5707d8d6c8f34ce59aa1855a42153b3703635881fe1cc4a037070846a938a9d124845e1f217fd63c15ed99c79cbee2646b4df8018addaf76289708587a2b182747423e490c3ec21b9fd78c69cd73f2d8985cf916fd50b24e23a1a722d61449a8cd2dea4d1e4d334b103fb311cd1bf5454e0a979c52e65c303baa96d70d655f9a0a300a7ba78c5cc1aa0234d88eefd9bb11e245615257b8955f031f76cec978ed0e3eb3c479b83b91bef4158728d"
login = "my-email-that-I-used-to-create-account-on-amocrm"
client_uuid = "e1899afa-85a8-45ca-93f9-7f4b26374a5d"
client_secret = "jUlw4E6H417MVuzsujIW39GXXjpLjratAwOEiaqPr3KDBKYwc9ZQLXK2UlhMcsuu"
headers = {
'login': login,
'client_uuid': client_uuid,
'client_id': client_uuid,
'client_secret': client_secret,
'grant_type': 'authorization_code',
'Authorization': f'Bearer {api_key}',
'session_id': "a8f1uutp26uipj414pfudab8cv",
'refresh_token': "def50200e673c8cf44d3c3a28df221ffb9d97c2f25cffefa09337953dc1fcc9b9aaa479235f8a6840cde697ecb58252452a22a7a71696665ff19a515db602601743799729b799a6d98d743ba33ab23b1d750ae479a068d16dd9d503062023a8406728c55afb37be5c4cf36df5a3c93bf06c9ff097c54db83d90308a92999669c80506590df2e609fa2aa2d55f3066d06efde55ab0a3c64c44a87743c5db3115632a6ecbd498cbc8012ccddfeabfe7ad9ad0f01664e72c434ccacd686be0e9582567ebdf6abb0cd4258c3b16bbb487222be774086398930ef18f2181a8b6d184a9ce61454710cc3bcaf01e978067a059aaa16c322c8d7b0e5cf34069b0f3ff1e2f5e5a665ec798d6f3d05bb95912528ef8b3230c3e2d03fbd1bbc4087b4bca54d050f876589b7c9fbc1c45199a58b021afa832776ed22637ea1e940dbb3156acec1c0b6320066a2045e26dcd5b6b169fb773ca11ebbb398b513ef8f6f04f973506a8c01c48dad944aadf078db881e8f7de959c187ae7cc41135e8c151bd89e5b8d71c2dc20ee6f282fefd8cb93fcd0f0942ab20ae457da41ea22fed3d881d21585219defc611531ca3909a632095e6c1d568f2408f4eab7b4b8aa90cb892d2253925b8682b9dce4f5",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImJkZDk0MTA1NTc5NWY0YzAwOTNmZDAwMmIxMDE1NDczZTZiN2ZjZDBlZGJlNWJlMWJjMjYzYTY2YTY2MGRlODA0OWQ5MjM5ZTE4ODY2MmQyIn0.eyJhdWQiOiI3YjQ5MGYxOC0yYmU1LTRhNTUtYTc0ZS0xOTYzYzk2NDFiOTgiLCJqdGkiOiJiZGQ5NDEwNTU3OTVmNGMwMDkzZmQwMDJiMTAxNTQ3M2U2YjdmY2QwZWRiZTViZTFiYzI2M2E2NmE2NjBkZTgwNDlkOTIzOWUxODg2NjJkMiIsImlhdCI6MTYzNzMzOTYzMywibmJmIjoxNjM3MzM5NjMzLCJleHAiOjE2MzczNDIwMzMsInN1YiI6Ijc2NDM4ODQiLCJhY2NvdW50X2lkIjpudWxsLCJzY29wZXMiOlsiY2hhdHMiLCJjcm0iLCJub3RpZmljYXRpb25zIiwidW5zb3J0ZWQiLCJtYWlsIl19.VEz7bIXpbsL-iMCz51UgW6JU1rySQTIXBLf_xPdBcmMpR3yIL8raUgCjEBJ05E05lhMsYxxwKEYg2yN468jkqfz5wTfmRdGs9ECiVatZ6oMEJYFUn7AF5JaP0fQw18raZdAo8CQJfZS86IuHEYpU2k-lW5LmDC2dIIyZYe4irnln0KRw5_mgci4JNPEyeqzYADI0deqX-rraXFjFm6UP4KkByfOrgfopyOV8cIBA_34i4CWewbVQLJF4-HWGuKvaOH5Erjh-GUNViyCCuH2Q4F7dI6gwhAd2z2lEbbTR_x4oyRx04QA1YOZjI64SJQAaaIYDFLphr7D4V59w44zG1g",
"csrf_token": "def50200e00c062021bac6b78d51f10f45cc28add00268d9",
}
response = requests.get(url, headers=headers)
pprint.pprint(response.json())
get_contacts()
I am reading their documentation (amoCRM) and I tried a lot of things but none worked! 😥
https://www.amocrm.com/developers/content/platform/abilities/
I followed their instructions how to work with their API:
https://nova-amocrm.notion.site/894277ffef5940e7a5daadd2abc746c8
But I am stuck and I don't know how to solve this problem!
Any help please?
If you don't know what is amoCRM, then

Firstly, you should create an integration in your amoCRM account and use your google colab notebook link to redirection (or any other link).
enter image description here
Next, use the request to get refresh code and access token:
data = {'client_id' : '3d31ef87-...',
'client_secret' : 'xyEFPbRH5xw1...',
'grant_type' : 'authorization_code',
'code' : 'def50200ec88c8fe724cfc58ebd7e19...',
'redirect_uri' : 'https://colab.research.google.com/drive/...'}
new_url = 'https://your_domain/oauth2/access_token'
request = requests.post(new_url, data=data)
request_dict = json.loads(request.text)
refresh_code = request_dict['refresh_token']
access_token = request_dict['access_token']
Next, define the request headers:
api_call_headers = {'Authorization': 'Bearer ' + access_token}
And after that you can get any information using filters:
api_statuses_response = requests.get('https://alamiri.amocrm.ru/api/v4/contacts?filter[id]={id}&filter[created_at][from]={from}&filter[created_at][to]={to}', headers=api_call_headers, verify=True)
print(api_statuses_response.json())

Related

Can't generate facebook access token

I need to generate the Facebook access token for a project.
Code:
import requests
import json
url = 'https://graph.facebook.com/oauth/access_token'
payload = {
'grant_type': 'client_credentials',
'client_id': 'clientid',
'client_secret': 'secret'
}
response = requests.post(url, json=payload)
print (response.json()['access_token'])
What I'm getting as output is my id|type, after research I found that it is due to the 'client_credential' request. I need to generate access tokens for a project (I want to use them to find likes etc). Any help would be appreciated, it's my first time attempting a project like this.
use [facebook-sdk.][1]
import facebook
app_id = 'YOUR_APP_ID'
app_secret = 'YOUR_APP_SECRET'
graph = facebook.GraphAPI()
# exactly what you're after ;-)
access_token = graph.get_app_access_token(app_id, app_secret)

Etsy API python image upload

does anyone use python for adding listings to Etsy using API?
I am using requests_oauthlib, but keep getting error when want to upload image to listing. Other functions, like listing creation etc. are working. Code of image upload:
self.oauth = OAuth1(self.api_key,
client_secret=self.api_secret,
resource_owner_key=self.oauth_token,
resource_owner_secret=self.oauth_token_secret)
def upload_picture(self):
url = "https://openapi.etsy.com/v2/listings/{listing_id}/images"
headers = {
"Content-Type": "multipart/form-data",
}
request = {
'image': ('img8.jpg', open('./img8.jpg', 'rb'), 'image/jpeg'),
}
r = requests.post(url=url, params=request, auth=self.oauth, headers=headers)
print(r.content)
error:
oauth_problem=signature_invalid
Thanks in advance!
I wasn't able to do it like that. I'm not sure why.
But below worked for me:
etsy = OAuth1Session(
ApplicationKey,
ApplicationSecret,
token, token_secret
)
url = "https://openapi.etsy.com/v2/listings/{listing_id}/images"
request = { 'image': open('./img8.jpg', 'rb') }
response = etsy.post(url, files = request)
I'm new to both OAuth & Etsy API

Posting to Wordpress Rest API fails to work in Python

Cannot seem to find nor create a minimum working example of posting an article to the REST API. I have the following set up and code.
import requests
import json
import base64
user = 'NASA'
password = 'bah bah ...'
url = 'http://any.com/wp-json/wp/v2'
data_sauth = user + ':' + password
post = {
'title': 'Rubbish',
'slug': 'rest-api-1',
'status': 'publish',
'content': 'random stuff',
'excerpt': 'lala',
'format': 'standard'
}
token = base64.standard_b64encode(data_sauth.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'application/json'}
r = requests.post(url + '/posts', headers=headers, json=post)
print(r.status_code)
print(json.loads(r.content))
Now I get a 200 code. But I just get no post appearing and return of the posts. I cannot find where this might be going wrong. The API reference is here: https://developer.wordpress.org/rest-api/reference/posts/#create-a-post
On the WP side, I have installed the plugin named Application Passwords and ensured that the credentials match. No post appears, I get a 200 status code, and get a poll from the WP site of the existing posts, but no new post.
I know this is super old but I tried your code with my credentials and it worked perfectly, check that you're requesting to the right URL (some sites have different URLs for the API).

Why python requests always return 401 on iMac?

I'm trying to verify whether resources are there in page source code. User should login first to go through the website. And all resources getting are binded with cookies.
I get cookies through selenium webdriver.get_cookies().
It's weird my requests.get(url, cookies) returns 200 on Windows while returning 401 on iMac. The below is my script:
dictCookies = driver.get_cookies()
jsonCookies = json.dumps(dictCookies)
with open('cookies.json', 'r', encoding='utf-8') as f1:
listCookies = json.load(f1.read())
mycookie = {
'userName': 'listCookies[0]['value']',
'token' : 'listCookies[1]['value']',
'userId' : 'listCookies[2]['value']'
}
And then I get cookies as below:
cookie = {
'userName': 'test',
'token' : 'DE233445DVF4',
'userId' : 'amanda'
}
r = requests.get(url, cookies=cookie)
print(r.raise_for_status())
It returns requests.exceptions.HTTPError: 401 Client Error: Unthorized for url: url.
Can anybody help this?

why isn't Requests not signing into a website correctly?

I am trying to sign into linkedin by using the requests library. After looking around the best way to do this is with using a requests.Session() I attempted to do this, but I was not successful.
I believe it has something to do with the link I post to.
import requests
payload = {
'session_key': EMAIL_GOES_HERE,
'session_password': PASSWORD_GOES_HERE
}
with requests.Session() as s:
s.post('https://www.linkedin.com/', data=payload)
#program should be signed in here so I am going onto a private page that requeires the user to be signed in.
r=s.get('https://www.linkedin.com/vsearch/p?f_CC=2289109')
#saving the results in an HTML file for easy debugging/viewing
html= open('testtest.html', 'w')
html.write(r.content)
html.close()
I should start with stating that you really should use their API:
http://developer.linkedin.com/apis
There does not seem to be any POST login on the frontpage of linkedin using those parameters?
This is the login URL you must POST to:
https://www.linkedin.com/uas/login-submit
Be aware that this probably wont work either, as you need at least the csrfToken parameter from the login form.
You probably need the loginCsrfParam too, also from the login form on the frontpage.
Something like this might work. Not tested, you might need to add the other POST parameters.
import requests
s = requests.session()
def get_csrf_tokens():
url = "https://www.linkedin.com/"
req = s.get(url).text
csrf_token = req.split('name="csrfToken" value=')[1].split('" id="')[0]
login_csrf_token = req.split('name="loginCsrfParam" value="')[1].split('" id="')[0]
return csrf_token, login_csrf_token
def login(username, password):
url = "https://www.linkedin.com/uas/login-submit"
csrfToken, loginCsrfParam = get_csrf_tokens()
data = {
'session_key': username,
'session_password': password,
'csrfToken': csrfToken,
'loginCsrfParam': loginCsrfParams
}
req = s.post(url, data=data)
login('username', 'password')

Categories