If you do this on the client side in JavaScript, you probably want to implement what is called an "implicit permission" stream. Azure has a process entry: https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-implicit/ .
Basically, your page will either have a “login” link or it will automatically go to the Azure authorization page with all the parameters encoded in the URL, for example, your client ID and areas that you request on the chart. If necessary, the user will have to log in, but in your case they may not need to. After the user logs in with their credentials (again, if necessary), they will be asked to provide consent to access your application. Assuming they say yes, Azure will redirect back to your page using an access token in the request hash. You will need to have a JS code to retrieve the access token from the hash. For example, a redirect would look something like this:
https://localhost/myapp/# access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q... &token_type=Bearer &expires_in=3599 &scope=https%3a%2f%2fgraph.microsoft.com%2fmail.read &id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q... &state=12345
<strong> pseudo-steps
- When loading the page, check if there is an access token in the URL fragment.
- If so, it indicates a download after authorization redirection, and you can start calling Graph.
- If not, you need to request the user or automatically redirect the authorization endpoint.
- You need to pass the token in the
Authorization http header when calling Graph. You can do this by adding the following line to your code (before send ): xmlhttp.setRequestHeader("Authorization", "Bearer " + token);
But why should I do this?
The answer is that OAuth works as an application, not as a user. Thus, the old model of authenticating the application as a user is not applied. The user must provide permissions to access his data.
So, really, what is happening here is not that the application should "log in again as a user", it should be auth as itself! This is really what you are doing (providing your client id). As part of this process, a user can log in to verify their identity, then provide consent.
Your client secret will not be used at all in an implicit stream. Essentially, your application will "prove" that it is truly on its own, using the client ID and being present on the URL that you register as part of registering your application.
source share