user3287495
user3287495

Reputation: 67

Use accessToken to obtain Dynamics Organization Uri

I have a nodejs project that is using adal-node (https://www.npmjs.com/package/adal-node) to authenticate via OAuth2 to Dynamics CRM. I have successfully gotten the authorizationUrl, then posted the code to get the accessToken. However, this last response does not return an instanceUrl. So I've been scowering the MSDN pages online for documentation on how to use the accessToken to obtain the correct organization instance Uri. I've tried this (https://msdn.microsoft.com/en-us/library/mt607485.aspx) and just get a 401 "Authorization has been denied for this request" response. Maybe I'm formatting this request improperly? Can someone kindly direct me to some documentation on how to use the valid accessToken I've obtained to retrieve the organization instance Uri for this user? Thanks.

Upvotes: 1

Views: 714

Answers (2)

ColinV
ColinV

Reputation: 91

Recently went through this myself, the issue I think you might be experiencing is the resource that is being requested access to when you request your authentication token is incorrect. While you would think it would be https://globaldisco.crm.dynamics.com/, I actually been successfully with https://disco.crm.dynamics.com/ (ensure to include the trialing slash) as the resource.

If your token is rejected then look at the WWW-Authenticate attribute in the response header and it will indicate the resource you should be requesting. Similar to this:

Bearer authorization_uri=https://login.windows.net/common/oauth2/authorize, resource_id=https://disco.crm3.dynamics.com/

After getting the right resource I was able to query the global disco service without issues and even though giving the NA disco resource returned instances from various regions. This worked on Azure AD tenants started in NA as well as the UK.

I have blogged a full sample here - http://colinvermander.com/2017/01/19/calling-the-dynamics-global-discovery-service/

Upvotes: 1

Gary Liu
Gary Liu

Reputation: 13918

According the description at https://msdn.microsoft.com/en-us/library/mt607485.aspx, to request against to https://globaldisco.crm.dynamics.com/api/discovery/v1.0/Instances(UniqueName='myorg'), you need to replace myorg to your own unique name of your dynamic crm server.

You can refer to https://www.dynamics-pros.com/support/kb/kb102033 for how to get the unique name.

Upvotes: 0

Related Questions