I thoroughly followed this guide in order to create an active directory and an application:
https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/
I added full application permissions for: Windows Azure Service Management API, Windows Azure Active Directory and Microsoft Graph.
In the Microsoft Azure Portal, in Azure AD B2C settings, I added a Sign in and Sign up policy.
The '/authorize' endpoint is accessed as follows:
https://login.microsoftonline.com/2d779d37-.../oauth2/authorize?p=[my_sign_in_policy]&client_Id=[application_client_id]&nonce=defaultNonce&redirect_uri=http://www.localhost:9000/&scope=openid&response_type=code+id_token&prompt=login
The [application_client_id] is copied from the CLIENT ID box in my application configuration.
The user I'm authenticating has Global Admin role.
After successful login I receive as hash parameters: "code" and "id_token".
The '/token' endpoint is accessed as follows:
Method: POST
https://login.microsoftonline.com/2d779d37-.../oauth2/token?grant_type=authorization_code
&client_id=[application_client_id]&code=[code_from_previous_step]&redirect_uri=http://localhost:9000/&resource=https://graph.windows.net/&client_secret=[application_client_secret]
I get the [application_client_secret] from the "keys" section in my application configuration.
When accessing the '/token' endpoint, I get the following error response:
400, Bad request
{"error":"invalid_grant","error_description":"AADSTS70000: Authentication failed: Authorization Code is malformed or invalid.\r\nTrace ID: 94f3ca04-2f33-402e-a80e-dc1147b1b49b\r\nCorrelation ID: 8fb2aebd-53ec-4c0b-8852-71fc8c6849ea\r\nTimestamp:
2016-07-15 07:11:13Z","error_codes":[70000],"timestamp":"2016-07-15 07:11:13Z","trace_id":"94f3ca04-2f33-402e-a80e-dc1147b1b49b","correlation_id":"8fb2aebd-53ec-4c0b-8852-71fc8c6849ea"}
What I have tried:
Removing application write permissions for Windows Azure Service Management API, Windows Azure Active Directory and Microsoft Graph.
Adding scope=openid%20offline_access parameter to '/token' endpoint.
Adding p=[my_signin_policy] parameter to '/token' endpoint.
None of which had any effect on the response.