GCM Unauthorized Error 401 - google-cloud-messaging

I'm not able to get response from GCM even i'm providing correct server API key generated by google.
Problem : I want to make a push notification using GCM from my sails app.
I'm posting below code to "https://android.googleapis.com/gcm/send"
Code : 1
{
"headers":{
"Content-Type":"application/json",
"Authorization":"key=xxxxxxxxxxxxxxxxxxxx"
},
"notification":{
"title":"Hello Notify",
"text":"Notification By Sails"
},
"registration_ids":["xxxxxxxxxxxx","xxxxxxxxxxxx","xxxxxxxxxxxx"]
}
Code : 2
{
"headers":{
"Content-Type":"application/json",
"Authorization":"key=xxxxxxxxxxxxxxxxxxxx"
},
"body":{
"notification":{
"title":"Hello Notify",
"text":"Notification By Sails"
}
},
"registration_ids":["xxxxxxxxxxxx","xxxxxxxxxxxx","xxxxxxxxxxxx"]
}
I'm getting response as below.
"statusCode": 401,
"body": "<HTML>\n
<HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n
<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n
<H1>Unauthorized</H1>\n
<H2>Error 401</H2>\n
</BODY>\n
</HTML>\n",

The headers should be HTTP headers, not element in the payload.

Make sure you have enabled the right API in Cloud Console. If sending with a Server API key, you need to have "Google Cloud Messaging for Android" enabled.

Related

HTTP Response Error with Post to LinkedIn using Share API v2

I am trying to post a Share to LinkedIn using OAuth v2 - I have got authorisation correctly and have the appropriate access keys.
This code is supposed to share a link on LinkedIn, but for some reason it's not working - I'm not sure why. Can anyone help?
this is my request body:
{
"distribution": {
"linkedInDistributionTarget": {}
},
"owner": "urn:li:person:XXXXXX",
"subject": "Test Share Subject",
"text": {
"text": "Hello !"
}
And this my call API shares :
publishPostLink(body : any, token : any){
this.headers = new HttpHeaders(
{
'Content-Type': 'application/json',
'Authorization':'Bearer '+token,
'cache-control': 'no-cache',
'X-Restli-Protocol-Version':'2.0.0', });
return this.http.post("https://api.linkedin.com/v2/shares" , body, {headers: this.headers});}
I get this issue:
I've already installed the Moesif CORS and it didn't worked
I fixed the error using this post..
it should use REST API from the backend and not from frontend
http://localhost is an insecure request origin so its not supportted in many cases.
Try using tunneling software like Ngork https://ngrok.com/

how to fix 'InvalidRegistration' FCM

I'm getting the InvalidRegistration error in response of my post request for sending push notification.
When I use the Firebase console to send push notification it works all fine.
so I copy the exact registration token I've used in console. I also get my api-key from my project-settings/cloud-messaging.
for sending the post request I've used postman.
I've sent my post request to https://fcm.googleapis.com/fcm/send
my postman header has this attributes:
key: Authorization value: key=<api-key-i-got-from-console>
key: Content-Type value: application/json
my body is:
{
"to" : "fDj8iG5ajZI:APA91bHfs_AjdxOc5Qn-1Plr_OfitrdCTchb4syhPGC0DhPMGiWggJ9mQ5W00ombqowlUSCUOfH_0N3KA-KF0VjQvj5mcMNUMUBgTblpsNzra60kwrbtH2ikfdBa8lenh__olBpGZD4O'",
"collapse_key" : "type_a",
"notification" : {
"body" : "plz work",
"title": "title"
},
}
I get this response from the Firebase
{
"multicast_id": 5801302802423881516,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
I did the same with fcm and django-push-notification and got the same result I have no clue what am I doing wrong since my token works with the console and I can send push notifications from the console.
it was my bad that I had an extra ' in the end of my token...

How to Properly Authenticate Google Vision API Using Polymer

I am trying to run a test on the Google Cloud Vision API to see how it fares to the client side Shape Detection API.
I am hoping to POST JSON with a base64 encoded image and get image text and barcodes returned.
I have created a GCP project and API key per the tutorial at (https://cloud.google.com/vision/docs/before-you-begin), but am getting an 401 error when trying to make requests.
error: {code: 401,…}
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"
The request is written in Polymer 2.x as follows:
<iron-ajax id="googleApi"
body="[[request]]"
content-type="application/json"
handle-as="json"
headers$='{"Authorization": "Bearer [[GOOGLE_API_KEY]]"}'
last-response="{{response}}"
loading="{{loading}}"
method="post"
url="https://vision.googleapis.com/v1/images:annotate">
</iron-ajax>
...
GOOGLE_API_KEY: {
type: String,
value: 'AIza0101010110100101101010'
}
...
getRequest(image) {
let encoded = image.toString('base64');
this.request = {
"requests": [{
"image": {
"content": encoded
},
"features": [{
"type": "LABEL_DETECTION",
"maxResults": 1
}]
}]
};
let request = this.$.googleApi.generateRequest();
request.completes.then(req => {
console.log('submission complete');
console.log(this.response);
})
.catch(error => {
console.log(error);
})
}
How do I resolve this authentication error?
It is an account admin issue? Improperly formatted code?
The authorization header is not needed, so the request should be in the form of:
<iron-ajax id="googleApi"
body="[[request]]"
content-type="application/json"
handle-as="json"
last-response="{{response}}"
loading="{{loading}}"
method="post"
url="https://vision.googleapis.com/v1/images:annotate?key=[[GOOGLE_API_KEY]]">
</iron-ajax>

unable to get response from an api

I a requesting an API to get response but it gives below some "JWT_TOKEN_MiSSING" response:
{
"response": {
"ids": "",
"time": 1505890122982,
"error": "JWT_TOKEN_MISSING",
"status": "failure"
}
}
API is: "https://www.viu.com/api/container/load?appid=viu_desktop&iid=9fa3fec0-9457-11e7-bee5-ed8c9b3d7f48&regionid=all&ver=1.0&aver=5.0&fmt=json&contentCountry=in&contentFlavour=telugu&languageid=en&platform=desktop&vuserid=viu-guest-9fa3fec0-9457-11e7-bee5-ed8c9b3d7f48&userid=guest-9fa3fec0-9457-11e7-bee5-ed8c9b3d7f48&ccode=IN&geo=2&id=playlist-24926470&start=0&limit=20&geofiltered=false"
I tried passing all the headers but no luck.
It seems that this API use JWT TOKEN or other tokens (such as HTTP BASIC AUTHENTICATION) to authenticate, you need to know this and add it to your request headers.

Twitter Insights API with Postman

I'm trying to read insights of my Twitter account with Twitter Insights API and when I request to that given end point I get and error called :
{
"errors": [
{
"code": "UNAUTHORIZED_ACCESS",
"message": "This request is not properly authenticated"
}
],
"request": {
"params": {}
}
}
This is the end point I tried :
https://ads-api.twitter.com/2/insights/accounts/:account_id/available_audiences
I used the given credentials like Consumer Key, Secret and Token, Token secret.
Can anyone explain me why I'm getting this error ? And what I should do ?
Here is a screenshot
Header:
You have to check box "Add params to header"
Everything else should be ok. Just check Consumer Key, Consumer Secret,... data