You need to sign in to do that
Don't have an account?

Auth.AuthToken.getAccessToken Returning Null
I am attempting to programmatically archive Attachments in Salesforce over to Google Drive. Google Drive API requires OAuth and I am having a little bit of trouble setting it up.
I was trying to avoid building and parsing HTTP Requests/Responses in order to get through the Authorization Flow but my attempts have failed.
First, I configured an API Project via the Google API Console
Second, I created an AuthProvider in Salesforce.

At this point, I would expect the following code sample to grab an Access Token for me:
For good measure, duplicated everything for a Google Auth Provider, instead of Open Id Connect with the same result. I also set up an external data source (which does authenticate and sync) to make sure I had a token.
Has anyone been able to successfully use AuthToken methods when connecting to Google APIs?
Is there anything that I missed along the way?
I was trying to avoid building and parsing HTTP Requests/Responses in order to get through the Authorization Flow but my attempts have failed.
First, I configured an API Project via the Google API Console
Second, I created an AuthProvider in Salesforce.
At this point, I would expect the following code sample to grab an Access Token for me:
system.debug(Auth.AuthToken.getAccessToken('0SO4B000000CacK', 'Open ID Connect'));However, I receive a null value.
For good measure, duplicated everything for a Google Auth Provider, instead of Open Id Connect with the same result. I also set up an external data source (which does authenticate and sync) to make sure I had a token.
Has anyone been able to successfully use AuthToken methods when connecting to Google APIs?
Is there anything that I missed along the way?
- Created a new API project to be able to start fresh
- Created the Auth Provider as seen in the screenshot
- Modified the Registration Handler to validate users based on FederationIdentifier
- Under Setup > Domain Management > My Domain I added Google as an authentication service
- Logged in to my org using Google as the Authenticator
I believe I was then able to call the method above and receive my access token. Let me know if this helps you out.When using Auth.AuthToken.getAccessToken you have to take care about three aspects:
1. The authProviderId (1st parameter) correspond to the 18-character identifier of your defined SSO Provider
2. The providerName (second parameter) must to be one of the expected values ("Facebook", "Salesforce", "Open ID Connect", "Microsoft Access Control Service", "LinkedIn", "Twitter", "Google")
3. The code must to be executed in the current user context (the user who loged into the external SSO provider), it should'nt be executed as System Admin or any other different user.
More info at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Auth_AuthToken.htm
Hi. I have a same problem.
I'm trying to get an access_token.
I'm call
Auth.AuthToken.getAccessToken('0SO1j00000002MdREA', 'Custom');
but i returns null.
I'm confused... Currently I created my custom auth provider. There is it's info:
String domainUrl = 'https://some-test-8689-dev-ed.cs102.my.salesforce.com';
String startUrl = 'https://mytestdomain.com';
Auth.AuthConfiguration authConfig = new Auth.AuthConfiguration(domainUrl,startUrl);
List<AuthProvider> authPrvs = authConfig.getAuthProviders();
System.debug(authPrvs);
USER_DEBUG [89]|DEBUG|authPrvs (AuthProvider:{DeveloperName=TestCognito, FriendlyName=TestCognito, Id=0SO1j00000002MdREA, ProviderType=Custom})
[12:18]
and just one moment: in my Custom Provider I have Id 0SO1j00000002Md - without 3 last characters
Simple Answer is: Open Authentication Provider, open on the OAuth-Only Initialization URL, login. Now you get the Accesstoken!