I would like to use the Google API to insert a secondary calendar. I've used the Google explanation but can't seem to do it myself. How do you use:
calendar = {
'summary': 'calendarSummary',
'timeZone': 'America/Los_Angeles'
}
created_calendar = service.calendars().insert(body=calendar).execute()
print created_calendar['id']
So if you want to add a secondary calendar this is the curl:
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer here_add_access_token" https://www.googleapis.com/calendar/v3/calendars -d {'summary':'here_calendar_name'}
And in Python is:
import requests
url = "https://www.googleapis.com/calendar/v3/calendars
headers = {
'Content-Type': "application/json",
'Authorization': "Bearer access_token"
}
payload = {
'summary': 'Google Calendar secondary',
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
response = response.text
Related
I have this request using a curl command and I want to translate it to python using the requests library
curl -X POST https://endpoint/prod/api/Translations/start \
-H 'Authorization: Bearer <accessToken>' \
-H 'Content-Type: application/json' \
-d '{ "text": ["first segment to translate.", "second segment to translate."], "sourceLanguageCode": "en", "targetLanguageCode": "de", "model": "general", "useCase": "testing"}'
You can use requests library.
The following curl:
curl -X POST "https://www.magical-website.com/api/v2/token/refresh/" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"refresh": "$REFRESH_TOKEN"
}'
I wrote in python the following way:
import requests
def get_new_token():
url = 'https://www.magical-website.com/api/v2/token/refresh/'
token = constants.THE_TOKEN
payload = f'{{ "refresh": "{token}" }}'
headers = {"accept": "application/json", "Content-Type": "application/json"}
print("Token handling ....")
r = requests.post(url, data=payload, headers=headers)
print(f"Token status: {r.status_code}")
return r.json()['access']
You can try this one.
import requests
url = "https://endpoint/prod/api/Translations/start"
payload = {
"text": ...,
"sourceLanguageCode": ...,
...
}
headers = { "Authorization": "Bearer ...", "Content-Type": "application/json" }
res = requests.post(url, data = payload, headers = headers)
print(res.status_code)
print(res.text)
I am trying to POST a multipart/base64 xml file to portal using the following in python. How can i run it in Python?
curl -X POST -H 'Accept: application/xml' -H 'Content-Type: multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>' --data-binary #<filename>.xml https://<customer_name>.host.com/cxml/invoices
You can use this website
I got this code. Could you try it ?
import requests
headers = {
'Accept': 'application/xml',
'Content-Type': 'multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>',
}
data = open('filename.xml', 'rb').read()
response = requests.post('https://<customer_name>.host.com/cxml/invoices', headers=headers, data=data)
Curl Code works python does not
trying to change from curl to python
curl --location --request PUT "https://api.getpostman.com/environments/XXXXX-YYYYYY-ZZZZ-BBBB-AAAA-ZZZZZZ?apikey=12334567890" \
--header "Content-Type: application/json" \
--data "{
\"environment\": {
\"values\": [
{\"key\": \"url\", \"value\": \"http://10.12.30.131\"}
]
}
}"
import requests
import json
url = 'https://api.getpostman.com/environments/XXXXX-YYYYYY-ZZZZ-BBBB-AAAA-ZZZZZZ?apikey=12334567890'
header = {"Content-type": "application/json"}
body = '{\"environment\": { \"name\": \"Prod - Deploy\", \"values\": [ {\"key\": \"url\", \"value\": \"http://10.12.30.131\"}]}}'
response = requests.put(url, data=json.dumps(body), headers=header)
print(response.status_code)
print(response.text)
expect a 200 response
the body should be body = '{"environment\": {"name\": "Prod - Deploy\", "values\": [{"key\": "url\", "value\": "http://10.12.30.131\"}, {"key\": "appPath\", "value\": "footytips-apis-v1\"} ]}}'
Have a CURL request like that:
curl -X POST "https://page.com/login"
-H "accept: application/json" -H "Content-Type: application/json"
-d "{ \"username\": \"admin\", \"password\": \"pass\"}"
In Python I guess it should look like this:
import requests
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = {'username': 'admin', 'password': 'pass'}
response = requests.post('https://page.com/login', headers=headers, data=data)
response
After this it gives me [502] error for bad gateway. What am I doing wrong with my python query and how it should be modified?
Try using:
requests.post(..., json=data)
When you use data= requests will send it form encoded, to actually put json in the body you have to use json=
I'm attempting to delete a track from a test playlist I've created. It's the third track in the playlist (ie index 2), with a Spotify id of 3PqvdKpjaxab0mWns1laKo. My code to do this is as follows:
method = "delete"
url = "https://api.spotify.com/v1/users/"+username+"/playlists/6bhZt1Uia5pfmeL5OYDJZM/tracks"
headers = {
'Authorization': 'Bearer ' + TOKEN,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
params = {
"tracks": [
{
"positions": [2],
"uri": "spotify:track:3PqvdKpjaxab0mWns1laKo"
}
]
}
response = requests.request(method, url=url, headers=headers, params=params)
However, when I run the code, the track is not deleted, and the following error message is returned: {'error': {'message': 'Missing tracks', 'status': 400}}
On the other hand, if I try the exact same request in the web api or using curl (as below), it works successfully.
curl -X DELETE "https://api.spotify.com/v1/users/USERNAME/playlists/6bhZt1Uia5pfmeL5OYDJZM/tracks" -H "Accept: application/json" -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" --data "{\"tracks\":[{\"positions\":[2],\"uri\":\"spotify:track:3PqvdKpjaxab0mWns1laKo\"}]}"
Any suggestions on what I'm doing wrong?
I found the solution: the data is passed to the endpoint as data, not as parameters. In other words, changing the last line to
response = requests.request(method, url=url, headers=headers, data=json.dumps(params))
fixes the problem.