You need to sign in to do that
Don't have an account?
Pedro Garcia 26
MailChimp connection with OAuth 2
Hi...
I want to access to MailChimp data with OAuth 2.
I've found the following example at https://forceblogs.com/salesforce-integration-with-mailchimp-using-oauth-2-0/ but it doesn't work.
The error is because the line return null
var myPageRef = component.get("v.pageReference");
I appreciate any help
This is my modified code:
Helper
Client Controller
cmp
ApexClass
Thanks,
I want to access to MailChimp data with OAuth 2.
I've found the following example at https://forceblogs.com/salesforce-integration-with-mailchimp-using-oauth-2-0/ but it doesn't work.
The error is because the line return null
var myPageRef = component.get("v.pageReference");
I appreciate any help
This is my modified code:
Helper
({ doInit : function(component,Event,helper){ var myPageRef = component.get("v.pageReference"); console.log('here in doinit'); console.log(myPageRef); var code = ''; if(myPageRef != null){ code = myPageRef.state.code; } console.log('this is the code'+code); var url = window.location.href; console.log(url); console.log(code); console.log(JSON.parse(myPageRef)); if(code!=null && code.trim().length!=null && code!=undefined){ var action=component.get("c.doFetchAccessToken"); action.setParams({ 'code' : code, 'redirectUri' : url.substr(0, url.indexOf('?')) }); console.log('code '+code); console.log('redirectUri '+url.substr(0, url.indexOf('?'))); action.setCallback(this, function(response){ var state =response.getState(); if(state==='SUCCESS'){ var res = response.getReturnValue(); component.set('v.accessToken',res); } }); $A.enqueueAction(action); } }, handleClick : function(component, event, helper) { var action=component.get("c.connectToMailchimp"); action.setParams({ }); action.setCallback(this, function(response){ var state =response.getState(); if(state==='SUCCESS'){ var res = response.getReturnValue(); //console.log('URL FOR Mailchimp LOGIN-'+res.url); if(res.Error === undefined) { console.log('Mailchimp Login Started'); window.open(res.url, '_top'); } else if (res.Error != undefined && res.Error != null){ } } else if (state === 'ERROR'){ console.log('Mailchimp Login Failed: ERROR'); } }); $A.enqueueAction(action); } })
Client Controller
({ doInit : function(component, event, helper) { console.log('doinit ... ...'); helper.doInit(component, event, helper); }, handleClick : function(component, event, helper) { helper.handleClick(component, event, helper); } })
cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:isUrlAddressable" access="global" controller = "MailChimpOAuth"> <aura:attribute name="accessToken" type="String" /> <aura:attribute name="pageReference" type="Object"/> <aura:handler name='init' value='{!this}' action='{!c.doInit}' /> <div class = 'slds-grid slds-wrap slds-align_absolute-center' style="background-color:white;height:15rem;width:25rem;"> <div class = 'slds-col slds-size_12-of-12 slds-align_absolute-center'> <lightning:button variant="brand" label="Connect To MailChimp" title="Base action" onclick="{! c.handleClick }"/><br></br> </div> <div class = 'slds-align_absolute-center'> Access Token is : {!v.accessToken} </div> </div> </aura:component>
ApexClass
public class MailChimpOAuth { public static String clientId; public static String clientSecret; @AuraEnabled public static Map<String, String> connectToMailchimp () { Map<String, String> returnMap = new Map<String, String>(); MailChimp_Config__c gmc = getConfig(); clientId = gmc.Client_ID__c; clientSecret = gmc.Secrect_ID__c; System.debug('logLevel'); try{ String redirectURI = 'https://speed-dream-189-dev-ed.lightning.force.com/lightning/n/Guije_Start'; String url= 'https://login.mailchimp.com/oauth2/authorize'; url+='?response_type=code&client_id='+clientId+'&redirect_uri='+ EncodingUtil.urlEncode(redirectURI, 'UTF-8'); returnMap.put('url',url); } catch(Exception ex) { system.debug('Exception Error'); system.debug(ex.getLineNumber()+':'+ex.getMessage()+':'+ex.getCause()); returnMap.put('Error','Exception error while generating url'+'Line:'+ex.getLineNumber()+':Message '+ex.getMessage()+': Cause '+ex.getCause()); } return returnMap; } public static GUIJE_MailChimp_Config__c getConfig(){ List<MailChimp_Config__c> gmc = [Select id, name, Client_ID__c, Secrect_ID__c, Code__c from MailChimp_Config__c]; return gmc.get(0); } @AuraEnabled public static String doFetchAccessToken(String code, String redirectUri){ MailChimp_Config__c gmc = getConfig(); clientId = gmc.Client_ID__c; clientSecret = gmc.Secrect_ID__c; if(!String.isBlank(code)){ gmc.code__c = code; update gmc; } System.debug('logLevel'); String getTokenEndpoint = 'https://login.mailchimp.com/oauth2/token'; String oAuthCode = code; String requestBody = 'grant_type=authorization_code&client_id='+clientId+'&client_secret='+clientSecret+'&redirect_uri='+EncodingUtil.urlEncode(redirectUri, 'UTF-8')+'&code='+code; String errorMessage =''; HttpRequest httpReq = new HttpRequest(); HttpResponse httpRes = new HttpResponse(); Http http = new Http(); httpReq.setMethod('POST'); httpReq.setEndPoint(getTokenEndpoint); httpReq.setBody(requestBody); try{ httpRes = http.send(httpReq); if(httpRes.getStatusCode() == 200){ Map<String, Object> response_Map = (Map<String, Object>)JSON.deserializeUntyped(httpRes.getBody()); system.debug('response: '+httpRes.getBody()); return String.valueOf(response_Map.get('access_token')); }else{ system.debug('Unexpected Error with Mailchimp API'+ 'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode()); } }catch(System.Exception e){ if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){ errorMessage = 'Unauthorize endpoint' +'Goto Remote Site Setting and add '+' '+ getTokenEndpoint +' Endpoint'; system.debug( errorMessage); //return null; }else{ errorMessage = 'Unexpected Error' +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode(); system.debug(errorMessage); //return null; } } return httpRes.getBody(); } }
Thanks,