Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Code used in tutorial for setting up Blogger API with Google Apps Script
function getService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('blogger')
// Set the endpoint URLs, which are the same for all Google services.
// Set the client ID and secret, from the Google Developers Console.
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
// Set the property store where authorized tokens should be persisted.
// Set the scopes to request (space-separated for Google services).
// this is blogger read only scope for write access is:
// Below are Google-specific OAuth2 parameters.
// Sets the login hint, which will prevent the account chooser screen
// from being shown to users logged in with multiple accounts.
.setParam('login_hint', Session.getActiveUser().getEmail())
// Requests offline access.
.setParam('access_type', 'offline')
// Forces the approval prompt every time. This is useful for testing,
// but not desirable in a production application.
.setParam('approval_prompt', 'force');
function authCallback(request) {
var bloggerService = getService();
var isAuthorized = bloggerService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this tab');
// Modified from
function bloggerAPI() {
var service = getService();
if (service.hasAccess()) {
var api = "";
var headers = {
"Authorization": "Bearer " + getService().getAccessToken()
var options = {
"headers": headers,
"method" : "GET",
"muteHttpExceptions": true
var response = UrlFetchApp.fetch(api, options);
var json = JSON.parse(response.getContentText());
for (var i in json.items) {
Logger.log("[%s] %s %s", json.items[i].id, json.items[i].name, json.items[i].url);
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment