How to make an API call to Youtube using meteor - meteor

I am trying to make an API call to
https://www.googleapis.com/youtube/v3/subscriptions
I'm trying to make it so when a user logs in using google-accounts they will be able to see their YouTube subscription list. It is currently not working at I am not sure why.
Im getting two errors in the console:
1.
www.googleapis.com/youtube/v3/subscriptions?part=subscriberSnippet&mine=true&key=MYKEY&part=snippet&mine=true:1
GET https://www.googleapis.com/youtube/v3/subscriptions?part=subscriberSnippet&…ne=true&key=MYKEY&part=snippet&mine=true 401 (OK)
2.
Object {statusCode: 401, content: "{↵ "error": {↵ "errors": [↵ {↵ "domain": "gl…e": 401,↵ "message": "Invalid Credentials"↵ }↵}↵", headers: Object, data: Object}content: "{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "authError",↵ "message": "Invalid Credentials",↵ "locationType": "header",↵ "location": "Authorization"↵ }↵ ],↵ "code": 401,↵ "message": "Invalid Credentials"↵ }↵}↵"data: Objectheaders: ObjectstatusCode: 401__proto__: Object
My code looks as followed:
//client side
Meteor.autosubscribe(function(){
var newUser = Meteor.user();
Meteor.subscribe('currentAccessToken');
});
var url = "https://www.googleapis.com/youtube/v3/subscriptions?part=subscriberSnippet&mine=true&key=key";
var options = {
'headers' : {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + "currentAccessToken",
'X-JavaScript-User-Agent': "Google APIs Explorer"
},
'params' : {
part : 'snippet',
mine : 'true',
}
};
var searchResult = HTTP.call("get", url,options,
function (error, result) {
if (!error) {
Session.set("twizzled", true);
}
console.log(result);
});
On the server side I am publishing the access token so that it can be sent with the GET request
//Server Side
Meteor.publish("currentAccessToken", function(){
return Meteor.users.find(this.userId, {fields: {'services.google.accessToken': 1}});
});
I greatly appreciate any help or documentation.

The first thing to check would be authorization scopes. You have to explicitly add YouTube scope (more than one, actually, each defining single action or a small bunch of actions). To do that, you can use Accounts global object. Just add this code anywhere on the client side (inside client folder):
Accounts.ui.config({
requestPermissions: {
google: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.upload'
]
},
requestOfflineToken: {
google: true
}
});
The next time you authorize with Google, you should see that the popup asks not only for basic information but for YouTube access. For more information about YouTube Google API scopes, see official docs.

Related

Getting ugcPost details through rest/posts api?

I could not get the ugcPost of a post or comment, the urn looks like: urn:li:ugcPost:7023566176156811264,7023567581345140736
how to use this in the api rest/posts/{ugcPosts urn}
the API returns the error {
"message": "Could not find entity",
"status": 404,
"code": "NOT_FOUND"
}
I have also added the headers
{
Linkedin-Version: 202210,
X-RestLi-Protocol-Version: 2.0.0
}
but it is still returning the same error even though I have encoded the urn, example:
https://api.linkedin.com/rest/posts/urn%3Ali%3AugcPost%3A7023566176156811264?viewContext=Reader
You need to add a bearer token in the header. And viewContext=Reader params is not required as per my knowledge of the documentation. I am sharing a sample example.
var axios = require('axios');
var config = {
method: 'get',
url: 'https://api.linkedin.com/rest/posts/urn%3Ali%3AugcPost%3A7023566176156811264',
headers: {
'X-Restli-Protocol-Version': '2.0.0',
'LinkedIn-Version': '202208',
'Authorization': 'Bearer [your_bearer_token]',
}
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
You can visit this link for more information.

How to get permissions / scope for Google Apps Script to Firebase Cloud Messaging HTTP v1 API?

I am trying to send a Firebase Cloud Message from Google Apps Script. I have followed the following questions & links & assembled the following code:
Get 403 response with the "new" Firebase Cloud Messaging API
The res|error I am receiving is:
{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
My Code in Google Apps Script is:
function sendFCM(text){
text = {
"message": {
"topic": "news",
"notification": {
"title": "Breaking News",
"body": "New news story available."
},
"data": {
"story_id": "story_12345"
}
}
}
var projectId = "xxxxxxx";
var apiUrl = "https://fcm.googleapis.com/v1/projects/"+projectId+"/messages:send";
var authKey = "AAAU.......Cs";
var token = ScriptApp.getOAuthToken();
var options = {
'method' : 'post',
'contentType': 'application/json',
'muteHttpExceptions' : true,
'payload' : JSON.stringify(text),
headers:{Authorization: "Bearer "+ token},
};
var res = UrlFetchApp.fetch(apiUrl, options);
Logger.log(res);
}
Script's properties > User Scopes list follwoing:
https://www.googleapis.com/auth/firebase.database
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/userinfo.email
FCM & GCM Api is enabled in project's developer console.
Edit:
From Ref's comment, I added the following scope to app manifest:
https://www.googleapis.com/auth/firebase.messaging
Upon Run, it asked me to authorize & now the response is an instance of a Message. But still I do not know how to define a to device i.e. to which device the message should go.
{
"name": "projects/send-xxxx3/messages/866xxxxx92"
}

Firebase.auth error with 400 bad request

I found a strange error while I developing system using Firebase with service url contains user data.
User data is below.
{
"uid": "kt9Hcp2FbYbBvvIeSHHa1RbvHcv2",
"displayName": "Anonymous 901",
"photoURL": null,
"email": null,
"emailVerified": false,
"identifierNumber": null,
"isAnonymous": true,
"providerData": [
],
"apiKey": "MyApiKeyString",
"appName": "MyAppName",
"authDomain": "my.auth.domain",
"stsTokenManager": {
"apiKey": "MyApiKeyString",
"refreshToken": "refreshTokenString",
"accessToken": "accessTokenString",
"expirationTime": 1532451863076
},
"redirectEventId": null
}
I encode the above anonymous user data and include it in the service url.
( http://myserviceurl?userdata=encodedUserData )
Inside the system receives that url, firebase creates a user object with that user data contained in the url.
The purpose of this url is to use specific user's information in any browser.
However, when I call that service url, sometimes system creates user object well, sometimes got error -
400 Bad request errors with
https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=MyApiKeyString
And error data is below,
{
"error": {
"code": 400,
"message": "TOKEN_EXPIRED",
"errors": [
{
"message": "TOKEN_EXPIRED",
"domain": "global",
"reason": "invalid"
}
]
}
}
Few hours later it works well, I changed nothing though.
I could not find the exact error point, but I suspect error occurs while observing authentication state or before this step.
Here is code snipets
#bind
private makeUserLoadingPromise(): Promise<void> {
let unSubscribe: () => void;
return new Promise<void>((resolve, _reject) => {
const onInitialized = this.makeOnInitializedAuthStateChanged(resolve);
unSubscribe = this.auth.onAuthStateChanged(onInitialized);
}).then(() => {
unSubscribe();
this.auth.onAuthStateChanged(this.onAuthStateChanged);
});
}
#bind
private makeOnInitializedAuthStateChanged(resolve: () => void) {
return (user: firebase.User | null) => {
this.user = user;
resolve();
};
}
#bind
private onAuthStateChanged(user: firebase.User | null) {
this.user = user;
}
Or maybe it relates with expirationTime?
I couldn't find any hints about this situation.
Any advice would be appreciated.
It is not clear what you are doing, but it appears that you are using the API incorrectly and insecurely. The plain user object contains a refresh token that is indefinite. Passing it around via URL is a really bad idea.
First don't rely on internal implementations, it is subject to change.
To get the user's information on your backend, the right way to do it, is to get the user's ID token using officially supported API, eg user.getIdToken(), then pass it to your server.
On your server, you verify it via the Firebase Admin SDK: admin.auth().verifyIdToken(idToken). Then you know this is a real authenticated user. If you need the full user info, you can then look it up using the decoded user id in the token: admin.auth().getUser(decodedIdToken.sub).

Firebase REST Auth on Android returns 500

Up until last week, the only resonable path available (for a lot of reasons) to use Firebase in a Xamarin.Android app required using Firebase.Xamarin. However, this appears to have stopped working as well.
Specifically, I had been able to successfully make the following call in my Android and iOS apps and get successful responses:
var contentString = $"grant_type=refresh_token&code=string&refresh_token={auth.RefreshToken}";
var requestContent = new StringContent(contentString, Encoding.UTF8, "application/x-www-form-urlencoded");
var uri = new Uri(string.Format("https://securetoken.googleapis.com/v1/token?key={0}", this.authConfig.ApiKey));
var response = await client.PostAsync(uri, requestContent).ConfigureAwait(false);
var responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
As of yesterday, I started getting the following responseData from the above code:
iOS:
{
"error": {
"code": 403,
"message": "Requests from this ios client application \u003cempty\u003e are blocked.",
"status": "PERMISSION_DENIED",
"details": [
{
"#type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/<my_apps_project_id>/apiui/credential"
}
]
}
]
}
}
Android:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "ipRefererBlocked",
"message": "The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions.",
"extendedHelp": "https://console.developers.google.com/apis/credentials?project=<my_apps_project_id>"
}
],
"code": 403,
"message": "The request did not specify any Android package name or signing- certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions."
}
}
After some research, I found this Google Cloud Endpoints Google Groups thread that said to be sure the following are in the http request's headers:
Header x-android-package
Label servicecontrol.googleapis.com/android_cert_fingerprint
Header x-android-cert
Label servicecontrol.googleapis.com/android_package_name
Header x-ios-bundle-identifier
Label servicecontrol.googleapis.com/ios_bundle_id
I added client.DefaultRequestHeaders.Add("X-Ios-Bundle-Identifier", <my_apps_bundle_id>); to my code before the client.PostAsync and it works like a charm.
Moving on to Android, I added "x-android-cert" and x-android-package to the request headers:
client.DefaultRequestHeaders.Add("x-android-cert", <my_apps_SHA1>);
client.DefaultRequestHeaders.Add("x-android-package", <my_apps_package_id>);
prior to my client.PostAsync call. However, now I get the following responseData:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
A moment of venting: For me, this is almost the last straw when it comes to Firebase. It has been over a month of overcoming one undocumented and half-baked API/SDK after another. It seems as if NOTHING works as advertised (including - off topic - getting Firebase working with an Angular4 project). Thank you for letting me share my frustration. Had I known how painful this was going to be, I would have setup my own MBaaS.
Before I give up on Firebase, I wanted to ask for help with this issue. Any insights and assistance would be greatly appreciated.
EDIT 1:*
I get similar results with the following changes:
var requestContent = new StringContent(postContent, Encoding.UTF8, "application/json");
var uri = new Uri(string.Format("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key={0}", this.authConfig.ApiKey));

Accessing the Youtube API using Meteor and percolate's google-api package

I've just started my first meteor project and I'm trying to access youtube's developer API.
I've created developer credentials with google, and I've included accounts-ui and percolate's google-api packages. I can successfully login with accounts-ui which means it appears my OAuth settings are working.
I then try to run something like this on the client:
GoogleApi.get('youtube/v3/search',{
part : 'snippet',
q : 'cats',
maxResults : 25
},
function(err,data) {
!err ? console.log(data) : console.log(err);
});
And I get the following error on the console:
Error: failed [403] { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } }
I'm not sure if I'm calling the function incorrectly, as I can't seem to find any usage examples of the GoogleApi.get() function (and I'm a meteor beginner), or whether my developer account is not properly setup, or what.
Any help or pointers you can pass along is greatly appreciated. Thanks!
Update:
Using FullStack's suggestion below, my final code ended up looking like this:
var url = "https://www.googleapis.com/youtube/v3/search";
var params = {
key: {Google API Key}
part: "snippet",
q: searchTerm,
maxResults: 25
};
Meteor.http.get(url, {params: params}, function (err, result) {
console.log(result.statusCode, result.data);
var retdata = result.data;
Session.set("youtubeSearchItems", retdata.items);
});
I recommend not using the google-api package and just doing the HTTP call yourself. Below is example code:
var url = "https://www.googleapis.com/youtube/v3/search";
var options = {
'headers' : {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + yourAccessToken,
'X-JavaScript-User-Agent': "Google APIs Explorer"
},
'params' : {
part : 'snippet',
q : 'cats',
maxResults : 25
}
};
var searchResult = HTTP.get(url, options);
Make sure you have the HTTP package installed: meteor add http
See official Meteor docs on HTTP for full details.
Percolate's package works just fine. The issue is that you have insufficient permissions as it states. To fix this, you have to add the scopes that are necessary for the APIs you wish to use.
if (Meteor.isClient){
var scopes = [
'https://www.googleapis.com/auth/youtube',
];
Accounts.ui.config({
requestPermissions: {google: scopes}
});
}
I didn't know which YouTube API you were referring to so I went with the Data one. There's also Analytics and Live Streaming. I got the above scope from this page:
https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps

Resources