Unable to access github API getting bad credentials error - github

I am trying to add a custom code check for a PR. After doing some research I found out that it can be done using the API mentioned below.
POST /repos/{owner}/{repo}/check-runs
Initially, it was giving me this error:
{
"message": "You must authenticate via a GitHub App.",
"documentation_url": "https://docs.github.com/rest/reference/checks#create-a-check-run"
}
I followed the guideline provided in this link.
I created a GitHub app.
Gave it required permission.
Generated a private key.
Generated a JWT token using the private key.
Installed the Github app in the repo too
I created a curl request:
curl --location --request POST 'https://api.github.com/repos/X/X-app/check-runs' \
--header 'Accept: application/vnd.github.v3+json' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.X.X-X-SAFvDnSkaJDjMI2T_BAC2iLlRZ7uNyFSe-X-UgFBFjoFrwsbcYFKfDM8f3FNPYpA6afhr18DLZ6rzu35klA' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "loremipsum"
}'
But, now I am getting this error
{
"message": "Bad credentials",
"documentation_url": "https://docs.github.com/rest"
}
I am not sure what I am missing here.

I figured this out. The GH documentation is a bit unclear/misleading. Here are the steps to make this work:
with the JWT bearer token, list your installations and note the installation id for your app
$ curl -i \
-H "Authorization: Bearer YOUR_JWT" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations
then get an installation access token for the above id
$ curl -i -X POST \
-H "Authorization: Bearer YOUR_JWT" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/:installation_id/access_tokens
then with that token create the check run but use "Authorization: token" header
curl -i -H "Authorization: token YOUR_INSTALLATION_ACCESS_TOKEN"

Related

What permissions is my github PAT missing?

I'm triggering a github action using a webhook. Until recently this worked perfectly but I got a request from Github to refresh my PAT to the new format and now I get a permission denied error. Am I missing a permission or has the authorization syntax changed?
This is the webhook I'm calling:
curl -d '{"ref":"v2.0","inputs":{"oga_no":"1369"}}' -H 'Accept: application/vnd.github.v3+json' -H 'Authorization: Basic ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' -H 'Content-Type: application/json;charset=utf-8' -X POST 'https://api.github.com/repos/ogauk/boatregister/actions/workflows/publish.yml/dispatches'
And this is the response:
{
"message": "Must have admin rights to Repository.",
"documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}
Something must have changed with the new token format. I spotted a post with a slightly different syntax and tried it.
curl -d '{"ref":"v2.0","inputs":{"oga_no":"1369"}}' -H 'Accept: application/vnd.github.v3+json' -H 'Authorization: token ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' -H 'Content-Type: application/json;charset=utf-8' -X POST 'https://api.github.com/repos/ogauk/boatregister/actions/workflows/publish.yml/dispatches'
So changing Basic to token fixed my problem.

How to delete Keycloak realm via REST API

I'm not able to delete realm via REST API on Keycloak 8.0.2.
My Keycloak runs on localhost:38080. I followed this documentation https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_realms_admin_resource, see "Delete the realm" paragraph. First, I got token for admin user at http://localhost:38080/auth/realms/master/protocol/openid-connect/token
curl --location --request POST 'localhost:38080/auth/realms/master/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=admin' \
--data-urlencode 'password=admin' \
--data-urlencode 'client_id=vga_tests' \
--data-urlencode 'grant_type=password'
Then I used the token and tried to delete the example realm by this call
curl --location --request DELETE 'localhost:38080/auth/Example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/json' \
--data-raw ''
(Note: curl commands are exported Postman calls)
I got this 404 response
{
"error": "RESTEASY003210: Could not find resource for full path: http://localhost:38080/auth/Example"
}
What am I doing wrong?
The documentation is incorrect, the address I needed to send the DELETE request to is localhost:38080/auth/admin/realms/example
curl command:
curl --location --request DELETE 'localhost:38080/auth/admin/realms/example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/x-www-form-urlencoded'
Alternatively, using the CLI interface (search for "deleting a realm"),
you can do:
$ kcadm.sh delete realms/example

How to add a label to an issue using GitHub API?

I'm trying to find a way to add a label to a GitHub issue using the API. After checking the API documentation I tried the following curl request:
curl -X POST -H "Authorization: token OOOOOOOOOOOOOOOO" -H \
"Accept: application/vnd.github.symmetra-preview+json" \
-d #label.json https://api.github.com/repos/CHSUNSONG/star-platform/issues/11
label.json contains:
["submitted"]
However, I got the following response:
{
"message": "Invalid request.\n\nFor 'links/1/schema', [\"submitted\"] is not an object.",
"documentation_url": "https://developer.github.com/v3/issues/#edit-an-issue"
}
Why isn't this working and how can I fix it?
You're POSTing to the wrong URL. Add /labels onto the end:
curl -X POST -H "Authorization: token OOOOOOOOOOOOOOOO" -H \
"Accept: application/vnd.github.symmetra-preview+json" \
-d #label.json \
https://api.github.com/repos/CHSUNSONG/star-platform/issues/11/labels

JSON_PARSER_ERROR on PATCH when updating a custom object

I am attempting to update a custom object using the Salesforce REST API as described here, but I consistently receive this 400 response
[
{
"message": "The HTTP entity body is required, but this request has no entity body.",
"errorCode": "JSON_PARSER_ERROR"
}
]
I have tried appending ?_HttpMethod=PATCH to the url and switching to a POSTcall, but while that yields 200 OK, it doesn't actually update the object. The object is "updateable" and I do have permission to edit it. Editing it directly in Salesforce works without issues.
Here is my (cleaned) request, as exported from Insomnia [Version 5.14.9 (5.14.9.1895)].
curl --request PATCH \
--url https://myInstance.salesforce.com/services/data/v20.0/sobjects/CUSTOMOBJECT/CUSTOMOBJECTID \
--header 'authorization: Bearer token' \
--header 'content-type: application/json' \
--data '{
"Additional_Information__c": "Test additional information"
}'
Any ideas on how I can resolve this?
According to the Salesforce documentation for sending HTTP requests with cURL, either a JSON data file needs to be sent or a ".json" extension needs to be appended to the URI.
curl --request PATCH \
--url https://myInstance.salesforce.com/services/data/v20.0/sobjects/CUSTOMOBJECT/CUSTOMOBJECTID.json \
--header 'authorization: Bearer token' \
--header 'content-type: application/json' \
--data '{
"Additional_Information__c": "Test additional information"
}'

Request to VSTS REST API only works on Postman

I'm trying to run this request
curl -X POST \
'https://*****.visualstudio.com/DefaultCollection/_apis/wit/wiql?=&api-version=1.0' \
-H 'authorization: Basic *****' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: *****' \
-d '{
"query": "SELECT [System.Id] FROM WorkItems"
}'
but I keep getting this error
{"count":1,"value":{"Message":"A value is required but was not present in the request.\r\n"}}
It works as expected on Postman, so I think the request and the server are OK.
I'm trying to follow the first example shown here: https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql
Am I missing something?
The URL is wrong, remove =& from the REST API url and the url will be like this:
https://*****.visualstudio.com/DefaultCollection/_apis/wit/wiql?api-version=1.