+ Start a Discussion
TJMeadowsTJMeadows 

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 ConsoleUser-added image

Second, I created an AuthProvider in Salesforce.
User-added image

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?
 
BerginBergin
Hi, I'm facing the same issue. Did you resolve this issue? Thanks.
TJMeadowsTJMeadows
I believe I did, but I long abandoned that project. My project scope was to transfer massive amounts of files to Google Drive from the Attachment object. I will say that some of restrictions on Salesforce callouts was crippling to the implementation, so I created an external solution. However, I can tell you the setup I left off with when I last dealt with this project.
  1. Created a new API project to be able to start fresh
  2. Created the Auth Provider as seen in the screenshot
    1. Modified the Registration Handler to validate users based on FederationIdentifier
  3. Under Setup > Domain Management > My Domain I added Google as an authentication service
    1. 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.

 
Franklin YsaccisFranklin Ysaccis
Hi, I was facing the same issue, but after read carefully the documentation I could solve it.
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
Vadym BombykVadym Bombyk

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

Chuck TomlinsonChuck Tomlinson
I believe the docs are wrong -- you put the Auth Provider NAME, not TYPE
Chuck TomlinsonChuck Tomlinson
or maybe that's only because mine's custom? but I was getting null, and now I'm not...