7

I'm developing a new managed package that will include a connected app. I created a connected app in a scratch org and committed it to version control. Then, when pushing the source to a new scratch org, I encountered the following error:

The consumer key is already taken.

I assume that this is because every time a connected app's source file is pushed to a new org, a new connected app is created and they obviously cannot share a consumer key.

My question is; in which org should I create the connected app if it needs to be used in all the scratch orgs that are used for development? Do I need to create an unmanaged package in a traditional development org and install it in each new scratch org after pushing the rest of the project's source?

I could create a new connected app in each scratch org but this wouldn't be viable for testing because the web server calling out to Salesforce would need to know the app credentials beforehand-- it couldn't reference multiple new credentials for each new connected app.

2 Answers 2

4

Connected apps can't be created as an unmanaged package.

Technically you need the connected app only in your Dev Hub and all other orgs were you at deploying metadata.

Scratch orgs can be authenticated from the same machine or command line that created them.

I would recommend creating managed packages for the connected app as it eases deployment and installing in orgs where you want to connect like production org.

You will also need to assign the connected app to the profile that's used in deployment for the rest of the orgs.

Connected Apps Deployment in Scratch orgs

you can have the connected app included in the scratch org metadata. Just make sure you exclude the oauthConfig.consumerKey.

For deployment via Metadata API consider the guidelines outlined here.

4
  • Is it correct that any org can authenticate with a connected app's credentials, regardless of whether the app has been installed in that org? Nov 6, 2017 at 20:11
  • Only Scartch org if created via the CLI .Others app need connected apps Nov 7, 2017 at 1:38
  • Do you have an idea in the case of Canvas App in Managed Package?
    – Prasanna
    Jan 25, 2018 at 7:24
  • 5
    i disagree somewhat - connected apps in scratch orgs are a valid use case since it's good practice to have dev mimic prod as close as possible. additionally there are some workarounds that require connected apps such as using github.com/financialforcedev/apex-mdapi with automated users/limited profile users (see gscloudsolutions.com/blogpost/…). it would be useful to be able to define these in code and have the key autogenerated when pushed to scratch orgs. Apr 9, 2018 at 19:35
6

I found the following in the Special Behavior in Metadata API Deployments:

Connected App

  • You cannot set the consumerKey in Metadata API. It is included in a retrieve operation for informational purposes. If you try to move the connected app to another org, you must remove the consumerKey from the .zip file before the deployment to an org. A new key will be generated in the destination org.

This makes sense. While you can retrieve the consumerKey if you need it via the Metadata API, you shouldn't be deploying it to each org. This includes scratch orgs.

So, you can have the connectedApp included in the scratch org metadata. Just make sure you exclude the oauthConfig.consumerKey.

Note: Even after excluding the consumerKey from the source I'm still running into challenges with subsequent pushes - Pushing a Connected App with the SFDX CLI results in “invalid cross reference id”

1
  • Agree with this approach and it should be the selected answer as its more in line with a proper DX approach.
    – doz87
    Jan 28, 2020 at 1:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .