简体   繁体   中英

How to get data from amoCRM through API?

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()) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM