Skip to content

Instantly share code, notes, and snippets.

@loganvolkers
Last active September 8, 2016 04:40
Show Gist options
  • Save loganvolkers/83db8b04049b7eb9bc649d14647da5dc to your computer and use it in GitHub Desktop.
Save loganvolkers/83db8b04049b7eb9bc649d14647da5dc to your computer and use it in GitHub Desktop.

Revisions

  1. loganvolkers revised this gist Sep 8, 2016. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion Auth0Mailchimp.js
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,27 @@ function doAuth(access_token, ctx, callback) {
    catch (e) {
    return callback(new Error('Failed Mailchimp parsing profile JSON' + e + 'body: ' + b));
    }
    callback(null, profile);
    // See: https://auth0.com/docs/user-profile/normalized
    var auth0Normalized = {
    // "email": profile.email,
    // "email_verified": true,
    // "family_name": "Foo",
    // "gender": "male",
    // "given_name": "John",
    // "identities": [{
    // "provider": "Mailchimp",
    // "user_id": profile.account_id,
    // "connection": "MailchimpDev",
    // "isSocial": true
    // }],
    // "locale": "en",
    "name": profile.account_name,
    "nickname": profile.username,
    // "picture": "https://lh4.googleusercontent.com/-OdsbOXom9qE/AAAAAAAAAAI/AAAAAAAAADU/_j8SzYTOJ4I/photo.jpg",
    "user_id": profile.account_id
    };
    var final = Object.assign({},profile,auth0Normalized);
    callback(null, final);
    });
    });
    }
  2. loganvolkers revised this gist Sep 8, 2016. 1 changed file with 35 additions and 22 deletions.
    57 changes: 35 additions & 22 deletions Auth0Mailchimp.js
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,39 @@
    function(access_token, ctx, callback) {

    request.get('https://login.mailchimp.com/oauth2/metadata', {
    'headers': {\
    'Authorization': 'OAuth ' + access_token,
    'Accept': 'application/json',
    'User-Agent': 'Auth0'
    }
    }, function(e,r,b){
    if( e ) return cb(e);
    if( r.statusCode !== 200 ) return callback(new Error('StatusCode:'+r.statusCode));
    var meta = JSON.parse(b);

    request.get(meta.api_endpoint, {
    'headers': {\
    function doAuth(access_token, ctx, callback) {
    var opts = {
    'headers': {
    'Authorization': 'OAuth ' + access_token,
    'Accept': 'application/json',
    'User-Agent': 'Auth0'
    }
    }, function(e,r,b){
    if( e ) return cb(e);
    if( r.statusCode !== 200 ) return callback(new Error('StatusCode:'+r.statusCode));
    callback(null,JSON.parse(b));
    });
    });
    }
    };

    request.get('https://login.mailchimp.com/oauth2/metadata', opts, function(e, r, b) {
    if (e) {
    return callback(new Error('Failed Mailchimp Metadata Lookup' + e));
    }
    if (r.statusCode !== 200) {
    return callback(new Error('Failed Mailchimp Metadata Lookup StatusCode:' + r.statusCode));
    }
    try {
    var meta = JSON.parse(b);
    }
    catch (e) {
    return callback(new Error('Failed Mailchimp Metadata parsing JSON' + e));
    }
    request.get(meta.api_endpoint + '/3.0', opts, function(e, r, b) {
    if (e) {
    return callback(new Error('Failed Mailchimp User/Account Lookup' + e));
    }
    if (r.statusCode !== 200) {
    return callback(new Error('Failed Mailchimp User/Account Lookup StatusCode:' + r.statusCode));
    }
    try {
    var profile = JSON.parse(b);
    }
    catch (e) {
    return callback(new Error('Failed Mailchimp parsing profile JSON' + e + 'body: ' + b));
    }
    callback(null, profile);
    });
    });
    }
  3. loganvolkers revised this gist Sep 8, 2016. 1 changed file with 17 additions and 5 deletions.
    22 changes: 17 additions & 5 deletions Auth0Mailchimp.js
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,25 @@ function(access_token, ctx, callback) {

    request.get('https://login.mailchimp.com/oauth2/metadata', {
    'headers': {\
    'Authorization': 'Bearer ' + access_token,
    'Authorization': 'OAuth ' + access_token,
    'Accept': 'application/json',
    'User-Agent': 'Auth0'
    }
    }, function(e,r,b){
    if( e ) return cb(e);
    if( r.statusCode !== 200 ) return callback(new Error('StatusCode:'+r.statusCode));
    callback(null,JSON.parse(b));
    if( e ) return cb(e);
    if( r.statusCode !== 200 ) return callback(new Error('StatusCode:'+r.statusCode));
    var meta = JSON.parse(b);

    request.get(meta.api_endpoint, {
    'headers': {\
    'Authorization': 'OAuth ' + access_token,
    'Accept': 'application/json',
    'User-Agent': 'Auth0'
    }
    }, function(e,r,b){
    if( e ) return cb(e);
    if( r.statusCode !== 200 ) return callback(new Error('StatusCode:'+r.statusCode));
    callback(null,JSON.parse(b));
    });
    });
    throw new Error("Context:"+ JSON.stringify(ctx));
    }
  4. loganvolkers created this gist Sep 8, 2016.
    14 changes: 14 additions & 0 deletions Auth0Mailchimp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    function(access_token, ctx, callback) {

    request.get('https://login.mailchimp.com/oauth2/metadata', {
    'headers': {\
    'Authorization': 'Bearer ' + access_token,
    'User-Agent': 'Auth0'
    }
    }, function(e,r,b){
    if( e ) return cb(e);
    if( r.statusCode !== 200 ) return callback(new Error('StatusCode:'+r.statusCode));
    callback(null,JSON.parse(b));
    });
    throw new Error("Context:"+ JSON.stringify(ctx));
    }