function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Robin Barnwell 3Robin Barnwell 3 

Defining the OAuth connection for an Apex Callout

I want to connect Salesforce to Mailchimp so I can send an Apex Callout with a REST PUT request to update a subscriber in Mailchimp.  I've connected Postman (https://www.getpostman.com/) to Mailchimp and go the core REST statement defined and working.  As part of this I needed to set-up the OAuth connection.  This was straight forward to do:
1. In Mailchimp, set-up the connecting App, this generates the Client ID and Client Secet to use.
2. In Postman, request a new Access Token, as shown
User-added image
3. In Postman, use the Access Token as authentication in the REST Header, like this

GET /3.0/Lists/ HTTP/1.1
Host: us16.api.mailchimp.com
Authorization: Bearer c36db89lkjh8hkh8l6ae0005bfc3

I'm really struggling to set-up the same for Salesforce.....  
1. Connected Apps aren't relevant as that is for apps that wish to get data from Salesforce, I want the other way around
2. Named Credentials seems to be the right place but this asks for an Authentication Provider, 
User-added image

3. The Authentication Provider picklist doesn't include Mailchimp, unless there is a "generic" one to use
User-added image
So to get it to work I've had to hard code the Access Token I got for Postman into the request in the Apex Callout as shown

public class Mailchimp {

    public static string  getMailChimp() {          
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://us16.api.mailchimp.com/3.0/Lists/'); 
    request.setHeader('Authorization', 'Bearer c36dbf7jhv89jbnjnkuf6a7a16ae0005bfc3');
    request.setMethod('GET');
    HttpResponse response = http.send(request);   
    string a = response.getBody();      
    system.debug('Here you go - ' + a);
    Return a;
    }     
}

What am I missing??
Best Answer chosen by Robin Barnwell 3
Robin Barnwell 3Robin Barnwell 3
It's OK everyone, I've solved it!!  You really do need to know your OAuth2 programming to get this baby set-up!!!

All Answers

Robin Barnwell 3Robin Barnwell 3
It's OK everyone, I've solved it!!  You really do need to know your OAuth2 programming to get this baby set-up!!!
This was selected as the best answer
Prathyusha Chitta 8Prathyusha Chitta 8
Hey Robin,

I'm stuck at similar situation. Can you share your solution on this ?

Did you write code in Apex class to get the auth token ?
Isha BansalIsha Bansal
I am stuck in the same position. Please share ,how you set Named credentials in this case?
Robin BarnwellRobin Barnwell
This is not easy, nor well documented, I'll put together a crib sheet and sharee
Rahul Agarwal 40Rahul Agarwal 40
Hi Robin, can you please share the updated code
ritu thatolaritu thatola
Hi Robin,

Can you share your findings.
Yohan DoleYohan Dole
Hi Robin,

Can you please share the code for oAuth2.

Regards,
Yohan
WiredLansWiredLans
If someone is still trying to do this connection.

First, you have to create an external Auth provider (https://help.salesforce.com/articleView?id=sso_provider_openid_connect.htm&type=5#register_application)
Secondly, you have to created a named credential (https://developer.salesforce.com/docs/atlas.en-us.224.0.apexcode.meta/apexcode/apex_callouts_named_credentials.htm) as Robin did.
And then you will be able to see your Auth provider in the lookup.