-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JS sample for calling YT Analytics API (v2)
- Loading branch information
1 parent
53a12c0
commit cdf1f87
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script src="https://apis.google.com/js/api.js"></script> | ||
<script> | ||
function authenticate() { | ||
return gapi.auth2.getAuthInstance() | ||
.signIn({scope: "https://www.googleapis.com/auth/yt-analytics.readonly"}) | ||
.then(function() { console.log("Sign-in successful"); }, | ||
function(err) { console.error("Error signing in", err); }); | ||
} | ||
function loadClient() { | ||
return gapi.client.load("https://youtubeanalytics.googleapis.com/$discovery/rest?version=v2") | ||
.then(function() { console.log("GAPI client loaded for API"); }, | ||
function(err) { console.error("Error loading GAPI client for API", err); }); | ||
} | ||
// Make sure the client is loaded and sign-in is complete before calling this method. | ||
function execute() { | ||
return gapi.client.youtubeAnalytics.reports.query({ | ||
"ids": "channel==MINE", | ||
"startDate": "2017-01-01", | ||
"endDate": "2017-12-31", | ||
"metrics": "views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained", | ||
"dimensions": "day", | ||
"sort": "day" | ||
}) | ||
.then(function(response) { | ||
// Handle the results here (response.result has the parsed body). | ||
console.log("Response", response); | ||
}, | ||
function(err) { console.error("Execute error", err); }); | ||
} | ||
gapi.load("client:auth2", function() { | ||
gapi.auth2.init({client_id: 'YOUR_CLIENT_ID'}); | ||
}); | ||
</script> | ||
<button onclick="authenticate().then(loadClient)">authorize and load</button> | ||
<button onclick="execute()">execute</button> |
cdf1f87
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you manage to get the code working?