Skip to content

Instantly share code, notes, and snippets.

@andrewroberts
Created January 29, 2016 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewroberts/e35e12f5fab84f4a1911 to your computer and use it in GitHub Desktop.
Save andrewroberts/e35e12f5fab84f4a1911 to your computer and use it in GitHub Desktop.
Google Apps Script for accessing NeverBounce RESTful API (and writing to a GSheet)
var VALID_COLUMN_INDEX = 4
var EMAIL_COLUMN_INDEX = 3
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu('Validate Emails')
.addItem('Validate Sheet', 'validateEmail')
.addToUi()
}
function validateEmail() {
var properties = PropertiesService.getScriptProperties().getProperties()
var API_SECRET = properties['API_SECRET']
if (typeof API_SECRET === 'undefined') {
throw new Error('You need to define a script property for the API Secret')
}
var API_USERNAME = properties['API_SECRET']
if (typeof API_USERNAME === 'undefined') {
throw new Error('You need to define a script property for the API Username')
}
var headers = {
"Authorization": "Basic " + Utilities.base64Encode(API_USERNAME + ":" + API_SECRET)
}
var payload = {
"grant_type": "client_credentials",
"scope": "basic user web",
}
var options = {
"headers": headers,
"method": "post",
"payload": payload,
}
var json = UrlFetchApp
.fetch('https://api.neverbounce.com/v3/access_token', options)
.getContentText()
var accessToken = JSON.parse(json).access_token
var dataRange = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Sheet1')
.getDataRange()
var data = dataRange.getValues()
var header = data.shift()
data.forEach(function(row) {
payload = {
"access_token": accessToken,
"email": row[EMAIL_COLUMN_INDEX],
}
options = {
"method": "post",
"payload": payload,
}
var json = UrlFetchApp
.fetch('https://api.neverbounce.com/v3/single', options)
.getContentText()
var response = JSON.parse(json)
var success = response.success
row[VALID_COLUMN_INDEX] = (success) ? response.result : response.msg
})
data.unshift(header)
dataRange.setValues(data)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment