0

I am trying to access the following url via Microsoft Graph Api :- https://graph.microsoft.com/v1.0/me I have used the code given below found on stackoverflow which should ideally give me JSON. But I am getting exception while running the code :-

try {
            String url_str = "https://graph.microsoft.com/v1.0/me";
            String access_token = getAccessToken();

            url = new URL(url_str);
            con = ( HttpURLConnection )url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setRequestMethod("GET");
            con.setRequestProperty("Authorization", access_token);
            con.setRequestProperty("Accept","application/json");
            con.connect();

            br = new BufferedReader(new InputStreamReader( con.getInputStream() ));
            String str = null;
            String line;
            while((line = br.readLine()) != null){
                str += line;
            }
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

I am getting valid access token. But I am getting following exception:-

java.io.FileNotFoundException: https://graph.microsoft.com/v1.0/me
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.controller.MicrosoftGraphController.getUserInfo(MicrosoftGraphController.java:50)
    at com.controller.MicrosoftGraphController.main(MicrosoftGraphController.java:82)

I tried searching but haven't found anything related to this particular issue. Any Suggestions? Thanks!

6
  • Possible duplicate of java.io.FileNotFoundException when I use Get Method in an HTTP Request Commented Feb 12, 2018 at 20:36
  • @MarcLaFleur-MSFT I am getting 404 response but still I am not able to figure out changes to be made in code to access the resource on server. Commented Feb 12, 2018 at 20:42
  • Can you add a capture of the HTTP traffic for this call? There should be a more detailed response than a simple 404 over the wire. Commented Feb 12, 2018 at 20:49
  • @MarcLaFleur-MSFT I am just debugging in eclipse... so i am not able to get any detailed response besides what I have mentioned. Commented Feb 12, 2018 at 20:58
  • You can use a tool like Fiddler or Wireshark to capture the traffic. Without seeing what is happening under the hood, it will be next to impossible to diagnose. Commented Feb 12, 2018 at 21:03

3 Answers 3

1

Change your code to be:

con.setRequestProperty("Authorization", "Bearer " + access_token);

You're using the OAuth2 protocol and it needs more than just the raw token. Note the space character after the word "Bearer" in the code.

Take a look at the docs for more detail.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I will post my answer and its something else that resolved my issue.
0

Because the service is returning an error instead of a valid response, I believe you will have to handle this in your code manually:

int httpClientErrorResponseCode = 400;
InputStream response;

if (con.getResponseCode() >= httpClientErrorResponseCode) {
    response = con.getErrorStream();
} else {
    response = con.getInputStream();
}
// Stream response or handle error

Comments

0

After looking at my code. I realized as I have created web app in azure portal and I am using the app credentials to get authentication token. So the url that I was trying was related to the personal account "https://graph.microsoft.com/v1.0/me" but after trying "https://graph.microsoft.com/v1.0/users/" I was able to access the graph api's. Auth token has nothing to do with this.

2 Comments

This isn't entirely correct, it is a token issue. The /me method works the same for both personal (MSA) and work/school accounts (AAD). What won't work however is signing in to a v1 App registered in the Azure Portal with an MSA. In order to use both MSA and AAD accounts, you need to created a Converged App registration at apps.dev.microsoft.com and use the v2 Endpoint.
@MarcLaFleur-MSFT Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.