Find all github repositories based on a specific keyword - github-api

I am trying to find all repos based on a keyword 'TERRAGRUNT_VERSION' using github search api and its returning the below error. Am I missing something in the url?
https://docs.github.com/en/rest/reference/search#search-repositories
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 3 column 1 (char 2)
Code:
import requests
token = "access_token=" + "23456789087"
base_api_url = 'https://api.github.com/'
search_final_url = base_api_url + 'search/repositories?q=TERRAGRUNT_VERSIONin:env/Makefile' + '&' + token
try:
response = requests.get(search_final_url).json()
for item in response['items']:
repo_name = item['name']
print(repo_name)
except Exception as e:
print(e)
The issue is I'm getting a 400 response.
I tried the below
response = requests.get("https://api.github.com/search/repositories?q=TERRAGRUNT_VERSIONin:env/Makefile&access_token=456744")
print(response)
Output:
<Response [400]>
{'message': 'Must specify access token via Authorization header. https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param', 'documentation_url': 'https://docs.github.com/v3/#oauth2-token-sent-in-a-header'}

In short, you have to pass access_token not as the url param but through the request header
If you try to search your link with browser, you would get the answer
{
"message": "Must specify access token via Authorization header. https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param",
"documentation_url": "https://docs.github.com/v3/#oauth2-token-sent-in-a-header"
}
Full description is on the link from response

Related

Python requests POST call with OAuth2 token returns 401

I have the following Python function which takes an access_token from my company's access token provider and hits the URL stored in matter_hydration_url:
def hydration_request(access_token, matter_hydration_url):
hydration_call_headers = {'Authorization': 'Bearer ' + access_token}
response = requests.post(matter_hydration_url, headers=hydration_call_headers)
response.raise_for_status()
I have tested BOTH access_token AND matter_hydration_url externally, through POSTMAN, and they both work. Unfortunately, in this code, the response variable has status_code field 401. I think I am mis-using requests.post(). Any ideas?
Worked after I changed Bearer in the String of line 2 to Access
¯_(ツ)_/¯

Google Cloud APIs - Python - Request contains an invalid argument

Using Google Cloud APIs and Oauth2, I am trying to list down projects and display IAM Policies for each project using my Python Desktop app. Sample code is below:
appflow = flow.InstalledAppFlow.from_client_secrets_file("client_secrets.json",
scopes=["https://www.googleapis.com/auth/cloud-platform"])
appflow.run_console()
credentials = appflow.credentials
service = googleapiclient.discovery.build(
'cloudresourcemanager', 'v1', credentials=credentials)
operation1 = service.projects().list().execute()
jason=json.dumps(
operation1,
sort_keys=True,
indent=3)
data = json.loads(jason)
#Gets the list of projects in a Python List object [Proj]
proj=[]
for mem in data['projects']:
print(mem['projectId'])
proj.append(mem['projectId'])
for prj in proj:
resource = 'projects/' + prj
response1 = service.projects().testIamPermissions(resource=resource, body=None, x__xgafv=None).execute()
response2 = service.projects().listOrgPolicies(resource=resource, body=None, x__xgafv=None).execute()
response3 = service.projects().getIamPolicy(resource=resource, body=None, x__xgafv=None).execute()
I get the similar error for all the 3 calls:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://cloudresourcemanager.googleapis.com/v1/projects/projects%2Fproject-name:testIamPermissions?alt=json returned "Request contains an invalid argument.". Details: "Request contains an invalid argument.">
Arguments appear to be correct. Does the service(cloudresourcemanager) version v1/v3 make a difference? Am I missing something? Thanks in Advance.
I think you should not need to parse the resource with projects/ like the HTTPS example because you are using the library that should be abstract this for you, so if you remove resource = 'projects/' + prj and try the call directly with the project id instead
response1 = service.projects().testIamPermissions(resource=prj, body=None, x__xgafv=None).execute()
response2 = service.projects().listOrgPolicies(resource=prj, body=None, x__xgafv=None).execute()
response3 = service.projects().getIamPolicy(resource=prj, body=None, x__xgafv=None).execute()
If it worked, you should no longer get error 400, but rather 403 "permission denied" because you are missing some of the scopes for those API calls(based on your code example).
The example google provided
testIamPermissions
listOrgPolicies
getIamPolicy

Access vault credentials using API

I am trying to access Vault credentials using Python API. I am doing something wrong as I always get access denied. I have used the following code:
def fetch():
url1="http://127.0.0.1:8200/v1/auth/gcp/config"
payload1={}
headers2={
'X-VAULT-TOKEN': 's.DsqQCKCY1JMhSe1k8A5rIyku'
}
try:
response1=requests.request("GET", url1, headers=headers2, data=payload1)
return response1.text
except Exception as err1:
return str(err1)
url="http://127.0.0.1:8200/v1/myengine/data/myspringapplication/staging"
payload={}
headers={
'X-Vault-Token': 'myroot'
}
try:
response=requests.request("GET", url, headers=headers, data=payload)
return response.text
except Exception as err:
return str(err)
when you are sending a request that contains a payload you should use the POST method. I would write it like this. If that doesn't work since the payload is empty maybe it is the GET request method so try removing data=payload.
import requests
payload = {}
# https://www.vaultproject.io/api/auth/gcp
url = "http://127.0.0.1:8200/v1/sys/auth"
ses = requests.session()
ses.headers.update({'X-VAULT-TOKEN': 's.Ezr0s63KQhW72knZHCIkjMHh'})
try:
r = ses.get(url, timeout=60)
if r.status_code == 200:
print(r.json())
else:
print("bad status code", r.status_code)
except requests.exceptions.Timeout:
pass
Edit: changed the code with default URL for testing, and it doesn't need a payload. So GET request is O.K. and this returns data for me.

How to get an user authentication token in a URL-Encoded form and receive a JWT Token using Python?

I'm trying to generate a user authentication token by posting the user name, password in a URL-encoded form, and received a JWT token.
I have tried using payload instead of header and both, but nothing works. I'm using Python 3.7.
import requests
headers = {'username':'username', 'password':'password'}
r = requests.post("https://sso.xxxxxx.com/sso-api/v1/token", headers=headers)
print (r)
I expected the output Response 200 Token generated successfully, but I'm receiving the error Response 404
I was able to to do it the the tips and help from xbound. Instead of header, I had to pass it as data. Then I confirmed that the response was ok and extracted the token.
import requests
payload = {'username':'*******#*********.com', 'password':'**********', 'grant_type':'password', 'scope':'openid'}
r = requests.post("https://***.******.com/***/**/token", data = payload)
##Confirm that reponse is OK!
r.status_code
print(r.status_code == requests.codes.ok)
##Decode json reponse - token
data = r.json()
##Select token
my_token = data['id_token']

'Error': 'JSON parse error - Expecting value: line 1 column 1 (char 0)' How do I fix this

I am writing a script to make requests from different web services. I am having problem when posting data from the json data below. When I run the
patient_create_bill()function I get the logs below from response.
RESPONSE IS!!!!!
{'Error': 'JSON parse error - Expecting value: line 1 column 1 (char 0)'}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 0.0.0.0:9000
DEBUG:urllib3.connectionpool:http://0.0.0.0:9000 "POST /api/bill/patient/bills/ HTTP/1.1" 400 72
Creating Patient Bill .....................................
I have attempted to make a POST on post man I get 201 response meaning the payload is fine there is nothing wrong with it.
This is my POST payload.
I have a separate file called mocks.py contains
has PATIENT_BILL_CREATE_PAYLOAD
PATIENT_BILL_CREATE_PAYLOAD = {
"bill_items": [{
"item": "Syringes",
"qty": 2,
"description": "Medicorp syringes"
}],
"bill_services": [{
"service": "Diagnosis",
"service_type": "1",
"duration": 5,
"description": "diagnosis"
}],
"client": "Sandra Hernandez"
}
This is the function
i've imported the PATIENT_BILL_CREATE_PAYLOAD and using it in this function.
def patient_create_bill(headers):
"""This function uses login creds provided and returns token plus logged in user data."""
url = "http://0.0.0.0:9000/api/bill/patient/bills/"
data = PATIENT_BILL_CREATE_PAYLOAD
res = requests.post(url, data=data, headers=headers)
res_data = res.json()
print("Creating Patient Bill .....................................\n")
return res_data
Your own answer is right (encode your data to json), and here is the code fixed. This worked for me:
instead of
res = requests.post(url, data=data, headers=headers)
the correct way to write it is...
import json
...
res = requests.post(url, data=json.dumps(data), headers=headers)
# or
res = requests.post(url, json=data, headers=headers)
More info about this type of requests in requests library docs.
My error was because of using data instead of json in request and also specified the header Content-Type as `application/json :-).
This log tells that you not received body in HTTP reponse (HTTP CODE 400):
DEBUG:urllib3.connectionpool:http://0.0.0.0:9000 "POST /api/bill/patient/bills/ HTTP/1.1" 400 72
Python trying to parse emtry string.
You can run this:
import json
json.loads('')
And this code will raise:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I think, you should check your URL endpoint to call.

Resources