Thank you sbelzile. I definitely will investigate the Application User feature further and understand what will be the biggest pain point if we choose to go this route. Meanwhile, I still try to get the authorization code flow experiment work as learning part of OAuth2.0. Can you take a look at what I am doing wrong.
This is the step I follow so far.
1. I have a crm environment (call it CRMEnv) which I have a global admin account. This crm environment is not linked to my company Azure AD. But it does have its own Azure AD subscription.
2. I also have an account in my company Azure AD. I registered a web app (call it 'TestCrmApp') there. I set the redirect url as http://localhost/. Set it as Multi-tenant. Under permissions I add Dynamics CRM online permission beside the default azure active directory permission. I create a key also.
3. I copy the following url to the browser and run it to request authorization to the CRM account:
login.microsoftonline.com(tenant id of CRMEnv)/oauth2/authorize?client_id=(Client Id of TestCrmApp)&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&response_mode=query&scope=https%3A%2F%2Fcrmenv.crm.dynamics.com%2F&state=12345
The browser redirect me to login page and I sign in using CRMEnv user/password. After I click sign in, I got the reply like this. The browser doesn't show a dialog to ask me to accept permission grant though.
localhost;state=12345&session_state=71e8e09e-cd3d-4480-80ce-01e9cfc73243
Next, I use this authorization code to request an access token.
POST login.microsoftonline.com(crmenv tenant id)/oauth2/token HTTP/1.1
Accept: application/json
Content-Type: application/x-www-form-urlencoded
client_id=(TestCrmApp client id)
&resource=https%3A%2F%2Fcrmenv.crm.dynamics.com
&grant_type=authorization_code
&code=authorization_code_received
&redirect_uri=http%3A%2F%2Flocalhost%2F
&client_secret=keycreatedinAAd
But I got the following error.
Unexpected response status: 400, message: {"error":"invalid_grant","error_description":"AADSTS70002: Error validating credentials. AADSTS70000: The provided access grant is invalid or malformed.\r\nTrace ID: d6ed51ae-3306-4847-836a-c3457ab20300\r\nCorrelation ID: d1554c9d-1e42-4c9f-bceb-f97e461d08ab\r\nTimestamp: 2017-06-01 21:40:18Z","error_codes":[70002,70000],"timestamp":"2017-06-01 21:40:18Z","trace_id":"d6ed51ae-3306-4847-836a-c3457ab20300","correlation_id":"d1554c9d-1e42-4c9f-bceb-f97e461d08ab"}
What am I doing wrong? The only thing I suspected right now is that my company Azure AD is not associated with an office 365 subscription as mentioned in this post, community.dynamics.com/.../212493.
Thanks,
Jay