I am working with an API from SignalHire. The API docs reference a callback URL but I don't have a very technical background and am not sure how to set this up. I've done some digging but I am still very confused and not sure how to proceed. Here is my code:
API_KEY = 'testapikey'
headers = {'apikey': API_KEY,
'callbackUrl': ''}
data = {'items': ['https://www.linkedin.com/in/testprofile/']}
response = requests.post("https://www.signalhire.com/api/v1/candidate/search", headers=headers, json=data)
if response.status_code == 200:
print(json.dumps(response.json(), sort_keys=True, indent=4))
I just need help understanding what a callback url is and how I can set that up.
When you post the search request, you won't get back results immediately. Instead, you'll get a 201 response that lets you know that SignalHire has received your request.
When the results are ready, they will be posted to the the URL you provide. It should be an endpoint that you write that sends a 200 response back to SignalHire acknowledging that it has received the search results.
Related
I'm working with Octoprint's API. I'm struggling trying to issue commands to the 3d printer.
For example, I want to issue a command that makes the 3d printer jog the X-axis.
import requests
headers = {"Authorization": "Bearer <token>"}
def Beep():
api_link = "http://octopi.local/api/printer/command"
params = {"command":"jog","x":10}
return requests.post(api_link, headers=headers, params=params)
The output of this code is <Response 400> (bad request). What I am missing?
From the API documentation:
If not otherwise stated, OctoPrint’s API expects request bodies and issues response bodies as Content-Type: application/json.
Your request is not sending JSON. Use this instead:
requests.post(api_link, headers=headers, json=params)
Also, it looks like the jog command is supposed to use the url path /api/printer/printhead.
I'm currently working on an app, one functionality of it being that it can add songs to the user's queue. I'm using the Spotify API for this, and this is my code to do so:
async def request():
...
uri = "spotify:track:5QO79kh1waicV47BqGRL3g" # temporary, can change later on
header = {'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': "{} {}".format(TOKEN_TYPE, ACCESS_TOKEN)}
data = {'uri': uri}
resp = requests.post(url="https://api.spotify.com/v1/me/player/queue", data=data, headers=header)
...
I've tried a lot of things but can't seem to understand why I'm getting Error 400 (Error 400: Required parameter uri missing).
so the Spotify API for the endpoint you're using suggests that the uri parameter required should be passed as part of the url, instead of as a data object.
Instead of data = {'uri': uri} can you please try adding your uri to the end of the url as such:
resp = requests.post(url="https://api.spotify.com/v1/me/player/queue?uri=?uri=spotify%3Atrack%3A5QO79kh1waicV47BqGRL3g", headers=header)
I also suggest using software like postman or insomnia to play around with the requests you send.
The Task##
A django application that allows users to sign up and once the user clicks on the account activation link, Zoho CRM is receiving the data and a contact is created in the CRM section.
The Problem
I am currently working on an absolute masterpiece - the ZOHO API.
I am struggling to set up the native Python code that uses POST/GET requests.
Regarding the zcrmsdk 3.0.0, I have completely given up on this solution unless somebody can provide a fully functional example. The support simply blames my code.
The documentation I consulted:
https://www.zoho.com/crm/developer/docs/api/v2/access-refresh.html,
https://www.zoho.com/crm/developer/docs/api/v2/insert-records.html
Since the post request in postman API works fine I do not understand why it does not work in python code
My approach
Generate an self-client API code on: https://api-console.zoho.com/
Insert that code on Postman and retrieve the access or refresh token
Use this access token in an add_user_contact function that is defined in the documentation
It works! Response is success and it is in Zoho CRM
The permsissions scope I am using is: ZohoCRM.modules.contacts.ALL, ZohoCRM.users.ALL, ZohoCRM.modules.deals.ALL, ZohoCRM.modules.attachments.ALL, ZohoCRM.settings.ALL, AAAserver.profile.ALL
Picture of Post Man POST REQUEST
My own Code
def authenticate_crm():
"""
access to response object id:
response_object.get('data')[0].get('details').get('id')
"""
url = 'https://accounts.zoho.com/oauth/v2/token'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
# one time self-client token here -
request_body = {
"code": "1000.aa8abec144835ab79b8f9141fa1fb170.8ab194e4e668b8452847c7080c2dd479",
"redirect_uri": "http://example.com/yourcallback",
"client_id": "1000.H95VDM1H9KCXIADGF05E0E1XSVZKFQ",
"client_secret": "290e505ec52685fa62a640d874e6560f2fc8632e97",
" grant_type": "authorization_code"
}
response = requests.post(url=url, headers=headers, data=json.dumps(request_body).encode('utf-8'))
if response is not None:
print("HTTP Status Code : " + str(response.status_code))
print(response.json())
I am essentially struggling to convert the Postman API request to a Python request to get the token as part of the workflow. What am I doing wrong here?
The documentation states: Note: For security reasons, pass the below parameters in the body of your request as form-data. (access-refresh link) but passing it in postman as form-data breaks the call completely.
According to their own documentation (which is convoluted, contradictory and full of outdated screenshots) the authentication key is needed only once.
Once the request from above runs, I would take the response in the third image and use the refresh key to add the contact.
I am also open to a solution with the SDK 3.0.0, if anybody can help.
I solved it!
I have changed this line:
response = requests.post(url=url, headers=headers, data=json.dumps(request_body).encode('utf-8'))
to this and added some return statement:
payload = '1000.6d9411488dcac999f02304d1f7843ab2.e14190ee4bae175debf00d2f87143b19&' \
'redirect_uri=http%3A%2F%2Fexample.com%2Fyourcallback&' \
'client_id=1000.H95VDM1H9KCXIADGF05E0E1XSVZKFQ&' \
'client_secret=290e505ec52685fa62a640d874e6560f2fc8632e97&'\
'grant_type=authorization_code'
response = requests.request(method="POST", url=url, headers=headers, data=payload)
if response is not None:
print("HTTP Status Code : " + str(response.status_code))
# print(response.text)
print(response.json())
# catch access and refresh token
at = response.json().get('access_token')
rt = response.json().get('refresh_token')
return at, rt
I do not understand why that is different but that fixed it and I could retrieve keys from ZOHO.
I have created a list of payload/param/data which need to pass as a param for every post request. I am using grequest to send the request parallelly. So the idea is for each parallel post request the param should be from the list. But what I am observing with my code is, the request is happening with the same payload from the list for every request, it is not accessing all the payload from the list.
I have tried two different ways, please help what is wrong with it, I am kind of lost because I have tested the same thing with different URLs and headers it works why is not working for param/data/payload for post request.
Below is my code:
for i in range(0, count):
list_payload_user.append(fp1())
url2 = "https:xxxxxyzzz/rest/v3/edit-user-requests/" + devid
headers = {'Authorization': token, 'Content-Type': 'application/json'}
#this 1 way I have tried:-
rs = [grequests.post(url2, headers=headers, data=final_pl1(), hooks={'response': res}) for i in range(0, len(list_payload))]
#here I have used a function - final_pl1(), which should get called every time, I have seen it is sending a different payload, but when grequest sending the request it is sending with 1st payload only.
#another way:-
rs = [grequests.post(url2, headers=headers, data=pl, hooks={'response': res}) for pl in list_payload_user]
#here I have used the list, grequest is sending with 1st payload from the list for every post request.
resp = grequests.map(rs, exception_handler=err_handler)
print('Maped response status code ==> ', resp)
Any help would be appreciated.
I have the following code for interacting with pull requests on the github api.
def merge(pull):
url = "https://api.github.com/repos/{}/{}/pulls/{}/merge".format(os.environ.get("GITHUB_USERNAME"), os.environ.get("GITHUB_REPO"), pull['number'])
response = requests.put(url, auth=get_auth(), data={})
if response.status_code == 200:
#Merge was successful
return True
else:
#Something went wrong. Oh well.
return response.status_code
def close(pull):
url = "https://api.github.com/repos/{}/{}/pulls/{}".format(os.environ.get("GITHUB_USERNAME"), os.environ.get("GITHUB_REPO"), pull['number'])
payload = {"state" : "closed"}
response = requests.put(url, auth=get_auth(), data=payload)
if response.status_code == 200:
#Close was successful
return True
else:
#Something went wrong. Oh well.
return response.status_code
Now merge works just fine, when I run it with a pull request, the pull request is merged and it feels good.
But close gives me a 404. This is strange since merge can clearly find the pull request, and also shows that I clearly have permissions set up properly so I can close the request.
I have also confirmed that I can close the request manually by logging in on github and pressing the 'close pull request' button.
Why does github give me a 404 for the close function but not the merge function? What is different between these two functions?
The answer is that the 'update a pull request' api call should be a POST request, not a put request.
Changing
response = requests.put(url, auth=get_auth(), data=payload)
to
response = requests.post(url, auth=get_auth(), data=payload)
Fixed the issue.