How to pass X-Auth-Token with Meteor's HTTP.call? - meteor

How to pass X-Auth-Token with Meteor's HTTP.call?
E.g. to do something like this:
curl -X GET \
--header "X-Auth-Token: 1234567890abcdeff978d137bc01a2" \
https://example.com/api/call

I found an answer on Meteor's forum:
options = {
headers: { 'X-Auth-Token' : '1234567890abcdeff978d137bc01a2' }
}
A bit more elaborated, in CoffeeScript:
res = HTTP.call 'GET', 'https://example.com/api/call',
headers:
'X-Auth-Token': auth_token
params:
a: 1
b: 2

Related

gRPC-Gateway: Is there a way to return responses in form of JSON arrays?

I'm making an implementation of https://todobackend.com/ using gRPC-Gateway. https://todobackend.com/'s spec requires some responses to be in form of JSON arrays, like:
GET /todos
=> [{ "title": "...", ... }, { ... }]
But AFAIK by using gRPC-Gateway I must return objects, like { "todos": [{ ... }, { ... }] }. Can I return arrays instead of objects?
I found this thread and got it working with response_body option along with allow_repeated_fields_in_body CLI argument.
rpc Add(Todo) returns (Todo) {
option (google.api.http) = {
post: "/v1/todos",
body: "*"
};
};
protoc -I proto/ -I googleapis \
--go_out ./proto --go_opt paths=source_relative \
--go-grpc_out ./proto --go-grpc_opt paths=source_relative \
--grpc-gateway_out=allow_repeated_fields_in_body=true:./proto --grpc-gateway_opt paths=source_relative \
./proto/todo/todo.proto
# note "allow_repeated_fields_in_body=true"

Translating curl command to Meteor's HTTP call

I try to "translate" this curl command
curl --request POST --header "Content-Type: application/json" --url http://some-url --user userName:apiKey --data '{ "some": "JSON data as string" }'
into Meteor's HTTP call. I tried this:
const options = {
{ "some": "JSON data as object" },
headers: {
'Content-Type': 'application/json',
},
params: {
user: 'userName:apiKey',
},
// Or alternatively
//
// user: 'userName:apiKey',
};
HTTP.call('POST', 'http://some-url', options, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
With curl command it works fine, with HTTP.call I get a 403, Forbidden. Authorization with userName:apiKey seems to fail. How do I specify the userName:apiKey in the HTTP.call example? Or maybe their is another problem?
If you need authentication, you need to add the auth parameter. The params parameter will actually set all the containing properties to be part of the POST request body.
const options = {
data: { "some": "JSON data as object" },
headers: {
'Content-Type': 'application/json',
},
auth: 'userName:apiKey'
}
Read: https://docs.meteor.com/api/http.html#HTTP-call

pendingToken in response payload of signInWithIdp

What does the pendingToken parameter mean, in the response payload of a successful POST to signInWithIdp in the Firebase Authentication REST API (Sign in with OAuth credential endpoint) ?
curl -X POST \
'https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key={{key}}' \
-H 'Accept: */*' \
.....
-H 'Content-Type: application/json' \
-H 'Host: identitytoolkit.googleapis.com' \
-d '{
"postBody": "id_token=eyJraWQiO....JWCJHHrxeg&providerId=apple.com",
"requestUri": "https://myapp.firebaseapp.com/__/auth/handler",
"returnIdpCredential": true,
"returnSecureToken": true
}'
{
"federatedId": "apple.com/ABCDE.abcde1234567895ab21ab098234.1234",
"providerId": "apple.com",
"email": "user#privaterelay.appleid.com",
"emailVerified": true,
"localId": "12345678abcdef",
"idToken": "eyJhbGciOiJSUzI1N...RiFQ",
"refreshToken": "AEu4I...N0DuQ",
"expiresIn": "3600",
"oauthIdToken": "eyJraWQiOiJB...Hrxeg",
"rawUserInfo": "{{...user info...}}",
"isNewUser": true,
"kind": "identitytoolkit#VerifyAssertionResponse",
"pendingToken": "AMzJoSn....jNlcw" <-------
}
pendingToken is a private property accidentally left in public header.
The team at Firebase are working to remove it, as they say
"It's a field of no use case at all".
You can read more about it here

How to send headers to an external API for authentication in Meteor

I'm a newbie trying to connect to my first API in Meteor.
The API docs (linked here) gave me a list of things I needed to generate (time-stamp, api-key & signature. I've generated them all and saved them as variables.) The docs say I need to make the following request:
curl -H "Request-Time: Wed, 06 Nov 2013 16:32:03 +0000" \
-H "Api-Key: 5d41402abc4b2a76b9719d911017c592" \
-H "Signature: 42d8824f24fb50e6793aa111c889b7df4d54bee9f5842a0d5fbca30cbfa469ae" \
http://wceaapi.org/v1.1/user/1234
How do I convert that in to Meteor Syntax?
Here's what I'm trying but I'm pretty sure it's totally wrong.
if (Meteor.isServer) {
Meteor.methods({
callAPI: function () {
this.unblock();
return Meteor.http.call("GET", "http://sandbox.wceaapi.org",
headers: {
"Request-Time" : timeStamp,
"Api-Key": key,
"Signature": hash});
}
});
}
//invoke the server method
if (Meteor.isClient) {
Meteor.call("callAPI", function(error, results) {
console.log(results.content); //results.data should be a JSON object
});
}

How to specify constraint in Meteor Http call?

I need to pass a where constraint(where UserName = "User1") in Meteor http call for Parse Rest APIs. Currently, the result that I get after the below API call includes all the entries not just those where UserName is User1.
var authUrl = "https://api.parse.com/1/classes/ImageData";
Meteor.http.call("GET", authUrl, {
headers: {
"X-Parse-Application-Id": "2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3",
"X-Parse-REST-API-Key": "9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu",
"content-type": "application/json"
},
params: {
"UserName": "User1",
}
}, function(error, result) {
console.log(JSON.parse(result.content));
}
);
The parse documentation for such an HTTP call in curl representation is:
curl -X GET \
-H "X-Parse-Application-Id: 2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3" \
-H "X-Parse-REST-API-Key: 9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu" \
-G \
--data-urlencode 'where={"UserName":"User1"}' \
https://api.parse.com/1/classes/ImageData
How can I correctly write this in Meteor?
This seems like it works:
var util = Npm.require('util');
var url = 'https://api.parse.com/1/classes/ImageData';
var result = HTTP.get(url, {
headers: {
'X-Parse-Application-Id': '2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3',
'X-Parse-REST-API-Key': '9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu',
'content-type': 'application/json'
},
query: 'where={"UserName":"User1"}'
});
console.log(util.inspect(result.data, {depth: null}));
Notes
Meteor.http.call is deprecated. Use the HTTP API instead. Note you will need to $ meteor add http.
Because you need a string and not a key/value pair, use query instead of params. For a GET, both are placed into the query string but your original code made the query ?Username=User1 rather than ?where....

Resources