Is it possible to used named credentials with a service account to connect to the Google Cloud Platform?

I'm trying to make a client to send Pub/Sub messages to Google Cloud from Apex, and it'd be great if I didn't have to worry about getting tokens.

This outlines how to get a token to make a request: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests

This is how to do this in Apex, but without using Named Credentials: https://salesforce.stackexchange.com/a/71442/7215

I don't see how I'd specify how to request tokens and create the proper JWT with Named Credentials and none of the examples I've seen that do use Named Credentials, seem to have a JWT-based flow.

  • Hey @jon_wu, did you manage to make it work ? I keep getting "Unable to proceed to token exchange" errors (or something like that). Sep 12, 2019 at 12:30
  • I got things working, but there's no "magic" to get the tokens for me. Instead, I just have a method like this to help out: private static HttpRequest createAuthenticatedRequest(String method, String endpoint) { String accessToken = getAccessToken(); HttpRequest req = new HttpRequest(); req.setHeader('Authorization', 'Bearer ' + accessToken); if (method == 'POST' || method == 'PUT' || method == 'PATCH') { req.setHeader('Content-Type', 'application/json'); } req.setMethod(method); req.setEndpoint(endpoint); return req; }
    – jon_wu
    Sep 12, 2019 at 16:55
  • hey @jon_wu I'm trying to do the same thing and I'm just learning about jwt tokens. How's the getAccessToken() method works exactly? Nov 27, 2020 at 18:48
  • 1
    That's API-specific. Think of it as a password. In this example getAccessToken() would just run code that returns a valid token for whatever API. Check out developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…. You can also cache this with org cache.
    – jon_wu
    Nov 27, 2020 at 22:42


You must log in to answer this question.

Browse other questions tagged .