Integrating MATLAB with Google BigQuery via REST API - matlab

I'm trying to use MATLAB's webread function to query Google BigQuery via the REST API, and am having issues. The following curl request is successful:
curl 'https://www.googleapis.com/bigquery/v2/projects/<MyProjectId>/queries' \
-X POST \
-H 'Authorization: Bearer <MyBearerToken>' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
--data-binary '{"query":"select count(1) from MyTable","useLegacySql":false}' \
--compressed
However, the following MATLAB code:
api = 'https://www.googleapis.com/bigquery/v2/';
access_token = '<MyAccessToken>';
options = weboptions('RequestMethod', 'post');
r = webread([api 'projects/<MyProjectId>/queries?access_token=' access_token], ...
'query', 'select count(1) from MyTable', ...
'useLegacySql', false, ...
options);
generates the following error:
Error using readContentFromWebService (line 45)
The server returned the status 400 with message "Bad Request" in response to the request to URL
https://www.googleapis.com/bigquery/v2/projects/<MyProjectId>/queries?access_token=<MyAccessToken>&query=select+count(1)+from+<MyTable>&useLegacySql=0
Entering the URL in the MATLAB error into a POST requester shows the following (generic) error, so it appears the URL is malformed:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter is missing"
}
],
"code": 400,
"message": "Required parameter is missing"
}
}
How can I use MATLAB's webread to accomplish the same thing as the above curl request?

It turns out that the webread function doesn't have this functionality. This issue is resolved successfully by using webwrite as follows:
options = weboptions( ...
'RequestMethod', 'post', ...
'MediaType', 'application/json');
data = struct( ...
'query', 'select count(1) from MyTable', ...
'useLegacySql', false);
r = webwrite([api 'projects/<MyProjectId>/queries?access_token=' access_token], ...
data, ...
options);

Related

How can I create a pull request with github api?

The API is : /repos/{owner}/{repo}/pulls.
I used the correct token, and the request body is :
data = {
"base": "master",
"head": "owner:master",
"title": "title",
}
The head is like this:
{'Accept': 'application/vnd.github.v3+json', "Authorization": "token {}".format(git_token)}
Then I call the pull API. Returned 200.
<Response [200]>
Why? Maybe the request body wrong?
The Pull Request API specified the answer:
Status: 201 Created
Try and anddapt their example to your repository, to see if it does work:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/pulls \
-d '{"head":"head","base":"base"}'

Getting "HTTP method not allowed, supported methods: GET" when request is successful

I am working on a Akka-Http project in which I am writing a POST endpoint http://localhost:8080/api/v1/internal/admin/import which requires a header with an access token.
My curl request is as follows
curl --location --request POST 'http://localhost:8080/api/v1/internal/admin/import' \
--header 'Content-Type: application/json' \
--header 'Accept: text/plain' \
--header 'Authorization: correct-token' \
--data-raw '{
"id": 100,
"name": "test"
}'
When the access token is incorrect, I get the http response in the exact format that is expected
"status": {
"code": 403,
"error": "authorization_error",
"details": "Invalid token"
}
}
But as soon as I provide a correct token in the header, the http request is successful, but I get a weird message HTTP method not allowed, supported methods: GET.
Just before sending the correct response, I tried to print the json and its correct. My code looks as follows :
onComplete(futureR) {
case Success(result) =>
println(s"Success response json is ${Json.toJson(result)}")
complete(result)
case Failure(ex) => complete(
500 -> StatusResponse(ApiStatus(500, None, None))
)
}
The result I am getting on terminal is Success response json is {"status":{"code":0}} which is correct, however in Postman, I keep on getting the error HTTP method not allowed, supported methods: GET.
Any pointers to this problem ? TIA

How can I authenticate with google cloud in Unity?

I'm trying to use google automl object detection in a Unity project.
Here is the google curl command example:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json" \
https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/models/model-id:predict \
-d '{
"payload" : {
"image": {
"imageBytes" : "/9j/4AAQSkZJRgABAQAAAQ … "
},
}
}'
After some googling, I start to create a UnityWebRequest to test response:
WWWForm form = new WWWForm();
var postUrl = "https://automl.googleapis.com/v1beta1/projects/1043345783500/locations/us-central1/models/IOD1798528344157847552:predict";
using (UnityWebRequest www = UnityWebRequest.Post(postUrl, form))
{
www.SetRequestHeader("Authorization", #"Bearer $(gcloud auth application-default print-access-token)");
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else if (www.isDone)
{
Debug.Log(www.downloadHandler.text);
}
}
So the response from google is:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
I understand that, I am not unauthenticated but couldn't understand how I can authenticate. Do I have to make an authentication request? How? Do I have to post something to get an access token? Then where should I send it?
I downloaded some credentials from google server but I really don't have any idea how to use it...

Salesforce Bulk Job CSV Upload via Google Apps Script UrlFetchApp

I'm attempting to use Google Apps Script to access Salesforce's Bulk API 2.0 and upload the CSV data to a job, however Salesfore is returning
[{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'GET' not allowed. Allowed are PUT"}]
Given my experience with the Salesforce API, I highly doubt the issue is actually with the method type, but that there is some other validation which is causing this error. However, upon inspection of the request body and comparing it to what I successfully submit through cURL, I don't see what I can do to resolve.
The request code:
var headers = {
"Authorization": "Bearer TOKEN"
};
var options = {
"method": "put",
"contentType": "text/csv",
"headers": headers,
"payload": "Id,Entity_Name__c\n001,One\n002,Two\n003,Three"
};
var url = INSTANCE + "/services/data/v44.0/jobs/ingest/" + JOB_ID + "/batches";
UrlFetchApp.fetch(url, options);
Google Apps Script request as returned via getRequest():
{
"headers": {
"Authorization": "Bearer TOKEN"
},
"method": "put",
"payload": "Id,Entity_Name__c\n001,One\n002,Two\n003,Three",
"followRedirects": true,
"validateHttpsCertificates": true,
"useIntranet": false,
"contentType": "text/csv",
"url": "INSTANCE/services/data/v44.0/jobs/ingest/JOB_ID/batches"
}
cURL request:
curl --request PUT \
--url INSTANCE/services/data/v44.0/jobs/ingest/JOB_ID/batches \
--header 'authorization: Bearer TOKEN' \
--header 'content-type: text/csv' \
--data '"Id,Entity_Name__c\n001,One\n002,Two\n003,Three"'
How can I resolve this?

Copy a public gdrive file to my account with curl

I tried to copy public gdrive files to my account by Gdrive API with a simple ps1 script and unix-tools for windows. (curl).
Worked out all required tokens and started to copy,download,delete files that I copied before.
The problem is, I found out that public shared files have different permissions
"capabilities": { "canCopy": true, "canEdit": false },
"editable": false, "copyable": true,
like this link for example:
drive.google.com/open?id=1ArIfiMSmBdlZQUm1cgHi8RRjhSCh8Rym
This is working fine by api request, but if I try to copy files with this permissions
"capabilities": { "canCopy": false, "canEdit": false },
"editable": false, "copyable": false,
drive.google.com/open?id=1cHAsF0D8zEp3LbVB4LmEQbJ3LdU8Cj7i
I get:
"reason": "forbidden",
"message": "Insufficient permissions for the specified parent.",
"locationType": "other",
"location": "parent.permissions" }],
"code": 403,
"message": "Insufficient permissions for the specified parent."
}
}
But in my browser I can rightclick taht file and simply make a copy.
So it seems just not possible by api requests.
But how can I do that by curl/http requests (with stored cookie or someting) ?
$UID = 'clientID.apps.googleusercontent.com'
$skey = 'clientsecret'
$readtoken = (Get-Content "$PSScriptRoot\refreshtoken.txt")
This is to create a fresh accesstoken from stored refreshtoken
$token = curl -d
"client_id=$UID&client_secret=$skey&refresh_token=$readtoken&grant_type=refresh_token"
https://accounts.google.com/o/oauth2/token | grep access_token
$string = $token.TrimStart(""access_token": `"")
$cleantoken = $string.TrimEnd("`",")
$drivefile = Read-Host -Prompt 'Put your Link here'
$driveID =
$drivefile.Substring(33)
$LINKS = curl --request POST
"https://www.googleapis.com/drive/v2/files/$driveID/copy" --header
"Authorization: Bearer $cleantoken" --header "Accept:
application/json" --header "Content-Type: application/json" --data
"{}" --compressed