Google Oaut2 - Python using requests whiout Library - python-3.x

I am trying to authenticate not google by means of requests if I use the api but I don't know what I put in the "code" field and it will work, you can help me, or the code follows below.
import requests
url = "https://accounts.google.com/o/oauth2/token"
payload='grant_type=authorization_code&client_secret=xxxxx&client_id=xxxx&redirect_uri=https%3A%2F%2Fpushsistemas.com.br%2F&code='
headers = {
'content-type': 'application/x-www-form-urlencoded',
'Cookie': 'NID=219=RgUebbUM4k3WX0uQr8HVcIZkQ5-rMrb4HHnfnRzNTxUse776sG6-LItgXwpdv-MmaFYf3Tu4otJN-UfP70OzDtdnfam2dwAZkd6yMsr8BW_7lGE4FTaas9QkoG-hOMqXgYEHvI4YK39dWgXhWnL0E5LcsOv0DXnfjJ2kUf7VlTs; __Host-GAPS=1:jGXTa0ipDEC58oJF7pZwNBVeSXCQRw:3IRHy3GC9mAq4nqt'
}
response = requests.request("POST", url, headers=headers, data=payload)
This output
{
"error": "invalid_request",
"error_description": "Missing required parameter: code"
}
if it is not possible to do this way could you send me how to do it using the google library?

Related

HP ALM API : Attaching file to run step through ALM REST API not working

We have fetched Run Steps details using ALM REST API.
(for reference ALM API Documentation
Same question is being asked in ALM community, Community Microfocus ALM
https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/run-steps
and passed Run Step id to below API and tried to attach file using POST and PUT.
https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/run-steps/<run-step-ID>/attachments
we are getting 404 response.
Bad request.
URL = "https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/run-steps/<step-id>/attachments"
METHOD = "PUT"
HEADERS = {
'cache-control': "no-cache",
"Slug": "some.html",
'Content-Type': "application/octet-stream"
}
def attach_to_run_step():
f = open("/path/to/some.html", "rb")
response = requests.put(URL, headers=HEADERS, cookies=cookies, data=f.read())
print(response.content)
Expected : 200 response
Actual : 404 status code
I think you need to use the following URL for your POST request:
https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/run-steps/<run-step-id>/attachments (remove /runs/<run-id>)
Update:
Here is an example, generated from Postman:
import requests
url = "http://xxx.xxx.xxx.xxx:8080/qcbin/rest/domains/DEFAULT/projects/demo/run-steps/1263/attachments"
payload="<file contents here>"
headers = {
'Content-Type': 'application/octet-stream',
'Slug': '1.jpg',
'Cookie': 'JSESSIONID=<session>; ALM_USER=2e69...; LWSSO_COOKIE_KEY=PTKYM...; QCSession=MTQwM...; XSRF-TOKEN=3dfa4...'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
P.S. We are developing commercial platform for integrating various test frameworks with ALM, which is called Agiletestware Bumblebee, so you might want to have a look at it.

pisignage API login error: "This username/email is not registered or active"

From there api docs, I have created a request to login and get the token. The code is under below, it returns 401 . The credentials are right and I can log in from web, but through API I can not.
import requests
import json
params_dict = {
"email": "example#mail.com",
"password": "example",
"getToken": True
}
response = requests.post(
'https://example.piathome.com/api/session',
data=params_dict
# headers={'Content-type': 'application/json', 'accept': 'application/json'} returns 500
)
json_response = response.json()
print(json_response)
Make sure that the API endpoint is set to username.pisignage.com (not just pisignage.com).
And the headers are absolutely required. Request and Response types must be set to application/json.

Error calling CF API login one time passcode

I am working with the CF API RESTful services. Trying to get an access token from cloud foundry's UAA API using https://login..../oauth/token web method.
I have verified that headers & body content is correct, but calling the api always returns a 400 error code with message missing grant type.
I have implemented this call in Objective-C, Swift & now Python. All tests return the same result. Here is my code example in Python:
import json
import requests
import urllib
params = {"grant_type": "password",
"passcode": "xxx"
}
url = "https://login.system.aws-usw02-pr.ice.predix.io/oauth/token"
headers = {"Authorization": "Basic Y2Y6", "Content-Type": "application/json", "Accept": "application/x-www-form-urlencoded"}
encodeParams = urllib.parse.urlencode(params)
response = requests.post(url, headers=headers, data=encodeParams)
rjson = response.json()
print(rjson)
Each time I run this, I get the response
error invalid request, Missing grant type
Any help would be greatly appreciated.
Your code mostly worked for me, although I used a different UAA server.
I had to make only one change. You had the Accept and Content-Type headers flipped around. Accept should be application/json because that's the format you want back, and Content-Type should be application/x-www-form-urlencoded because that's the format you are sending.
See the API Docs for reference.
import json
import requests
import urllib
import getpass
UAA_SERVER = "https://login.run.pivotal.io"
print("go to {}/passcode".format(UAA_SERVER))
params = {
"grant_type": "password",
"passcode": getpass.getpass(),
}
url = "https://login.run.pivotal.io/oauth/token"
headers = {
"Authorization": "Basic Y2Y6",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
}
encodeParams = urllib.parse.urlencode(params)
response = requests.post(url, headers=headers, data=encodeParams)
rjson = response.json()
print(json.dumps(rjson, indent=4, sort_keys=True))
I made a couple other minor changes, but they should affect the functionality.
Use getpass.getpass() to load the passcode.
Set the target server as a variable.
Pretty print the JSON response.
The only other thing to note, is that the OAuth2 client you use must be allowed to use the password grant type. It looks like you're using the same client that the cf cli uses, so if your UAA server is part of a standard Cloud Foundry install that is likely to be true, but if it still doesn't work for you then you may need to talk with an administrator and make sure the client is set up to allow this.

Python - Basic Authorization (OAuth2.0) to retrieve access token from API

I'm trying to generate an access token from an API using the following required information:
Authorization endpoint: https://api.paylocity.com/IdentityServer/connect/token
Authorization Header
The request is expected to be in the form of a basic authentication request, with the "Authorization" header containing the client-id and client-secret. This means the standard base-64 encoded user:password, prefixed with "Basic" as the value for the Authorization header, where user is the client-id and password is the client-secret.
Content-Type Header
The "Content-Type" header is required to be "application/x-www-form-urlencoded".
Other Values
The request must post the following form encoded values within the request body:
grant_type = client_credentials
scope = WebLinkAPI
I've tried using the Python 'requests' package without success, see below:
import requests
url = "https://api.paylocity.com/IdentityServer/connect/token"
headers = {
'Authorization': "Basic **********:**********",
'Content-Type': "application/x-www-form/urlencoded"
}
body = {
'grant_type': "client_credentials",
'scope': 'WebLinkAPI'
}
response = requests.request("POST", url, headers=headers, params=body)
print(response.text)
Instead of an access token, I receive an error message:
{"error":"invalid_client"}
I've verified that my base64 encoded username and password are correct, it looks something like "G/RaNdOm:MoReRaNdOm==" and when I add them to postman it works so my credentials are correct. What could be wrong?
I resolved this. There were two issues, the Authorization header was not using ASCII as the destination charset with base64 encoding. The other issue was trying to add the additional values to the parameters instead of the body in the requests package. The solution was to replace "params" with "data". Here was the solution:
url = "https://api.paylocity.com/IdentityServer/connect/token"
body = "grant_type=client_credentials&scope=WebLinkAPI"
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Authorization': "Basic ***(base64ASCII)username:password***",
}
response = requests.request("POST", url, data=body, headers=headers)
print(response.text)

SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed when using requests

I am trying to send data via API but am receiving a SSL Certificate Verify Failed error with the following code:
import requests
url = "https://urlhere"
payload = "name=Test&description=Test&deadline=2017-12-31&disableEmail=true&campaignFilterId=00ad9cac28fa4c29fdb641c64b007a0b&type=Manager"
headers = {
'x-csrf-token': "TVUmiEALzVRQDkoQWbzdLI6l6NgftGoU",
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache",
'postman-token': "f8a9c488-0402-be25-aefb-cb2df65897f3"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
I am aware of using "verify=False" but understand that is not a recommended work around. What could be wrong here? I don't think it is the server since when I run this same API call in Postman it runs without issue.

Resources