Accessing YouTube API in Meteor project for logged in user - meteor

I'm working on a new Meteor project which involves users logging into the site using their Google accounts through OAuth (I'm using the Meteor accounts-google package for this) and when signing in I need them to be able to see some data from the YouTube Analytics API for their YouTube channel. As of now the data I am trying to get is their total daily views, which I then hope to display on a chart for a specified time period.
I have added the following scopes to my accounts-google login system:
Meteor.loginWithGoogle({
requestPermissions: ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/youtube.readonly'],
requestOfflineToken: true,
forceApprovalPrompt: true,
loginStyle: "popup"
});
This all seems to be working very well, once a user signs into the site and grants the site access to these I can then see the necessary info in my MongoDB database. Under user.services.google I can now see it has accessToken, idToken, expiresAt, refreshToken, etc.
I've also decided to use the google api nodejs client by implementing it through the meteorhacks:npm package for Meteor. I am using this to refresh tokens (as seen in this SO answer I found helpful).
Using the "Try It" API Explorer on the YouTube Analytics API Documentation page, I can get the type of data I'm looking for through this request:
GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2015-10-01&end-date=2015-10-31&metrics=views&dimensions=day&sort=-day&key={YOUR_API_KEY}
Now is where I've been completely stuck and really unsure of where to go from here. How can I implement this into my site? I've tried for quite some time now to make this work but everything I attempt isn't working, and there's no real direction. If anyone is willing to help out I'd greatly appreciate it. I'm fairly new to Meteor/JS/APIs so any information/examples is extremely appreciated, especially noob friendly stuff! ;)

One way to do it is to use a method and the http package: https://atmospherejs.com/meteor/http
Looking at the doc you provide, you may try something like this:
Define your method on the server side
// server-side
Meteor.methods({
getYoutubeReports: function(channelId, accessToken, params) {
params.ids = "channel=="+ channelId;
params.key = accessToken;
return HTTP.get("https://www.googleapis.com/youtube/analytics/v1/reports", {
params: params
});
}
});
You can then call it on the client side with the data you get from your the authentication (ie. CHANNEL_ID_OF_MY_USER & ACCESS_TOKEN_OF_MY_USER)
// client-side
var reports,
myParams = {
"start-date": "2015-10-01",
"end-date": "2015-10-31",
"metrics": "views",
"dimensions": "day",
"sort": "-day"
};
Meteor.call('getYoutubeReports', CHANNEL_ID_OF_MY_USER, ACCESS_TOKEN_OF_MY_USER, params, function(error, result) {
// store or do stuff with the result of the HTTP request here
console.log(result);
});
Feel free to custom myParams as your user need!
And if you want to some more tips about how to use HTTP request (really useful to call external API), The Meteor Chef wrote a really good article about it : https://themeteorchef.com/snippets/using-the-http-package/
I hope it helps!

I ended up using the percolate:google-api package to handle my API call.

Related

Meteor js requestPermissions not working

Meteor js requestPermissions not working. I want to access google calendar, access token not permission with google calendar. How I can get access google calendar.
there are two things to consider. The right permission you need from google, and the API you use to access your data. You don't provide too many details so I don't know whether you need mobile (Cordova) too.
For your project, in Google Developer Console you need to enable the Calendar API. Once you do that, you have options to see what appId / authorization is relevant for your API so you add it to your Meteor Settings.
Then from the OAuth 2.0 scopes you need to select the exact scope you need (search for "calendar" for instance) and add that scope (the entire url) to your array of scopes.
Then you can do GET or POST with something like the native HTTP API of Meteor
example:
let data = HTTP.call('GET', `https://people.googleapis.com/v1/people/me/connections?pageToken=${res.data.nextPageToken}&personFields=emailAddresses`,
// let data = HTTP.call('GET', `https:https://www.googleapis.com/calendar/v3/calendars/{... your calendarId} `,
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: 'application/json'
}
},
(err, res) => { handle these })

Get location from Alexa Skills Kit (ASK)

I'm looking for a way to get the user's location, ideally longitude/latitude but address would work too, from the Alexa Skill Kit request to my custom skill. Also, I don't want to have to have the user link to an account on my app's side.
Is this possible? If so, how?
Amazon has now (2017-04-05) added this capability. See their blog post about their new Device Address API.
Using this new API you can get the address (either postal code or full address) of the device, as specified in the customer’s device settings.
From that you could use a geocoding API (such as is part of the Google Maps API) to translate the address into location coordinates.
As per this thread on the Amazon Developer forums, there is not currently (as of May 2016) a way to get user location via the publicly available APIs. The only skills able to do so, such as Uber or Domino's, are utilizing APIs that are not available through the Alexa Skills Kit. However, there's hope that it may be added, as "Jamie#Amazon" left this reply in that discussion:
Hey there,
Thanks for posting.
This has now been added to the roadmap. Thanks for the feedback.
Jamie
However, at the time of writing, no further update has been provided regarding the implementation of such a feature.
As #Tom has pointed out, it is now possible to get the device address in your custom skill. If you are using Python to create your skill, it's pretty easy to implement the new API. I've written a detailed blog post about it here. It also describes how to get the corresponding coordinates for the retrieved address, so it might be useful for you. In short, here is a Python function to get you started. I use the very handy Flask-Ask library for my skill. Thus, it is easy to get the value of the deviceId and consentToken objects. These are included by Alexa in the JSON request sent to your skill. They are needed for constructing the request to the Amazon address API endpoint:
import requests
from flask_ask import Ask, context
def get_alexa_location():
URL = "https://api.amazonalexa.com/v1/devices/{}/settings" \
"/address".format(context.System.device.deviceId)
TOKEN = context.System.user.permissions.consentToken
HEADER = {'Accept': 'application/json',
'Authorization': 'Bearer {}'.format(TOKEN)}
r = requests.get(URL, headers=HEADER)
if r.status_code == 200:
return(r.json())
The function will return something like this on a successful call:
{u'city': u'Berlin', u'countryCode': u'DE',
u'addressLine1': u'42 Unter den Linden',
u'addressLine2': None, u'stateOrRegion': u'Berlin',
u'districtOrCounty': u'Berlin', u'postalCode': u'10117',
u'addressLine3': None}
You can use geopy to convert this address to coordinates.
It is now possible to get the user's real-time geolocation (longitude/latitude) using the Alexa Location Services, which avoids having to integrate with a separate geocoding API as suggested by other answers. See the related blogpost for official information about this feature's release.
Provided that the device is compatible (context.System.device.supportedInterfaces.Geolocation exists), the location services are running and the alexa::devices:all:geolocation:read permission has been granted to your skill , you can retrieve a Geolocation object through the request's context, which will be equivalent to the following JSON payload:
"Geolocation":{
"locationServices": {
"access": "ENABLED",
"status": "RUNNING",
},
"timestamp": "2018-03-25T00:00:00Z+00:00",
"coordinate": {
"latitudeInDegrees": 38.2,
"longitudeInDegrees": 28.3,
"accuracyInMeters": 12.1
},
"altitude": {
"altitudeInMeters": 120.1,
"accuracyInMeters": 30.1
},
"heading": {
"directionInDegrees": 180.0,
"accuracyInDegrees": 5.0
},
"speed": {
"speedInMetersPerSecond": 10.0,
"accuracyInMetresPerSecond": 1.1
}
}
Please follow the below URL to Get location from Alexa Skills Kit (ASK)
URL:
https://api.eu.amazonalexa.com/v1/devices/{devicesId}/settings/address/countryAndPostalCode
Your Header would be something like below :
Host:api.eu.amazonalexa.com[keep your host you can get is from developer account during testing from json]
Accept:application/json
Authorization:Bearer [KEEP YOUR apiAccessToken here]
if request went success your will get response as below:
{
"countryCode": "IN",
"postalCode": "560102"
}
Make sure you have enabled permission in the Alexa app, and grants the permission of the respective skill please refer the bellow URL for more details of permission configuration
https://developer.amazon.com/blogs/alexa/post/0c975fc7-17dd-4f5c-8343-a37024b66c99/alexa-skill-recipe-using-the-device-address-api-to-request-information

meteor-shopify authenticator getPermanentAccessToken with code

I'm using the froatsnook:shopify atmosphere package to create an embedded public app on Shopify. I currently have a couple issues:
1) Getting the access token from the "code" query parameter after a user authenticates. As it mentions in the docs here, I'm supposed to use authenticator.getPermanentAccessToken(code) but what I don't understand is how to get call authenticator if the "code" parameter appears on the callback route (at that point, the authenticator I instantiated on the client pre-auth route is out of scope).
2) The "oAuth" function callback is never called for some reason, even when assigning it to Shopify.onAuth on the server.
3) The difference between post_auth_uri and redirect_uri ?
// I call this during 'onBeforeAction' for iron-router
function beforeAuth (query) {
// is this necessary..?
console.assert(Meteor.isClient);
// get shop name like 'myshop' from 'myshop.shopify.com';
const shop = query.shop.substring(0, query.shop.indexOf('.'));
// use api_key stored in settings
var api_key = Meteor.settings.public.shopify.api_key;
// Prepare to authenticate
var authenticator = new Shopify.PublicAppOAuthAuthenticator({
shop: shop,
api_key: api_key,
keyset: 'default',
embedded_app_sdk: true,
redirect_uri: 'https://45a04f23.ngrok.com/testContent',
//post_auth_uri: ???
// This is doesn't seem to be getting
// called after clicking through the OAuth dialog
onAuth: function(access_token) {
ShopifyCredentials.insert({
shop: shop,
api_key: api_key,
access_token: access_token
});
}
});
// Should i use something different with iron-router?
location.href = authenticator.auth_uri;
// how do i get code in this scope???
// authenticator.getPermanentAccessToken(code);
}
There are a few issues with the way you are trying to set up the authenticator, although it's not really your fault because the way Scenario 3 works in the docs is not an 'out of the box' solution and requires a bunch of custom code, including your own handler (I can provide a gist if you REALLY want to build your own handler, but I suggest using the new server-side onAuth callback instead)
1. Specifying a redirect_uri overrides the package's default redirect_uri handler which is Meteor.absoluteUrl("/__shopify-auth").
So instead, completely remove redirect_uri and put your testContent url in post_auth_uri instead.
2. ShopifyCredentials does not exist in this package. If you want to use it that way, make sure you actually have defined a collection called 'ShopifyCredentials' and insert the record from the server, not the client. Note that you will still need to add a keyset on the server for the API methods to work. If you are using user accounts and would like to permanently store credentials, I suggest saving the credentials to the database and adding the keyset via a server-side onAuth callback.
3. authenticator.getPermanentAccessToken(code) isn't useful unless you are using your own handler. Instead, you can just get access_token from the onAuth callback.
Also keep in mind that if you ever need to reauthenticate from inside the embedded app, you need to use window.top.location.href to break out of the iframe.
If you want a complete, working boilerplate example with user accounts see my gist here:
Authentication with Accounts and Persistent Keysets
If you aren't using accounts, you can use this gist instead, but please note that you really need to come up with some way to check that the current client has permission to request the keyset for a given shop before going to production:
Authentication with Persistent Keysets

How to use Google Contacts API in meteor?

I am using meteor to create a webpage with a dropdown list of Google Groups to select from and once selected, the Google contacts will be displayed.
I am using HTTP.call POST to Google's API and testing with the accessToken from mongoDB but when I use that token after some time it expires. I looked into implementing an authentication flow but it is getting very complicated since there is no sample code on Google for meteor. I am new to nodeJS, Javascript and Meteor. Am I going about this the wrong way? How would I implement this in meteor?
https://developers.google.com/accounts/docs/OAuth2?csw=1#expiration
To deal with the expiration of the accessToken, you will need to obtain the refreshToken from Google. With this refreshToken, you can obtain a new accessToken whenever necessary via a simple HTTP POST to Google's API. Here is the relevant documentation from Google. To obtain the refreshToken, you will need to request for offline access and may also need to force the approval prompt, as detailed in this SO post.
forceApprovalPrompt: {google: true},
requestOfflineToken: {google: true},
I recommend achieving all of the above using Meteor's HTTP package. All the tools are there. You've probably already figured it out:
var result = HTTP.post(
"https://www.googleapis.com/oauth2/v3/token",
{
params: {
'client_id': config.clientId,
'client_secret': config.secret,
'refresh_token': user.services.google.refreshToken,
'grant_type': 'refresh_token'
}
});
//Do some error checking here
var newAccessToken = result.data.access_token;
refresh_token - The refresh token returned from the authorization
code exchange.
client_id - The client ID obtained from the
Developers Console.
client_secret - The client secret obtained from
the Developers Console.
grant_type - As defined in the OAuth 2.0
specification, this field must contain a value of refresh_token.
result.data will be a JSON object with the following
{
"access_token":"1/fFBGRNJru1FQd44AzqT3Zg",
"expires_in":3920,
"token_type":"Bearer",
}
Have a look at this package its a little wrapper that does auto refresh for you:
here
I actually ended up building my own auth flow for with oauth handler because i needed to move away from a tokens linked to user profiles.

Error 'Login Required' while calling Google Analytics API from Google Spreadsheet

I use google scripts to fill Google Spreadsheet by Google Analytics data.
Problem: I get error: Login Required when I call =getCountOfNewUsers(B1,B2) from my Spreadsheet file. NOTE: when I run runTest() from google script project everything working OK.
FYI: I have followed this tutorial.
function runTest() {
try {
var results = getCountOfNewUsers(new Date(), new Date());
Logger.log(results.totalsForAllResults["ga:newVisits"]);
} catch(error) {
Logger.log(error.message);
}
}
function getCountOfNewUsers(date_from, date_till) {
date_from = Utilities.formatDate(date_from, 'GMT', 'yyyy-MM-dd');
date_till = Utilities.formatDate(date_till, 'GMT', 'yyyy-MM-dd');
var tableId = 'ga:*****';
// Make a request to the API.
var results = Analytics.Data.Ga.get(tableId, date_from, date_till, 'ga:newVisits', {});
if (results.getRows()) {
return results;
} else {
throw new Error('No views (profiles) found');
}
}
This is not solution to your question but it directs you to easy better solution to your problem if your interested!, you can easily setup this solution in an hour!
Details:
Better Than A writing our own script with Diligent Study and make Google scripts to fill Google Spreadsheet by Google Analytics data I use this Google Analytics Report Automation (Magic Script)
This Magic Script allow you to simplifies this process and makes it
easy to get the data you want so that you can focus on analysis and
reporting.
Custom API Dashboards - No Code Required it will save your burden for
Writing Your Own Script.
About the Error Try following things
Go to script editor, click on Resources > click on Use Google APIs.
Click on "Google APIs Console".
select APIs & auth > click on Registered apps.
Do Register your script project as web application.
select Certificate > Generate New Key .
This is an expected behavior. Custom Functions cannot ask users to give access to personal data. So they are not suitable for all types of services, specifically those which need authorization for personal data.
Check this url to know which services you can use - https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services
Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data

Resources