none
Getting 401 access error while trying to access office 365 mails RRS feed

  • Question

  • Hi ,

    I have created an application with Azure(its a paid one). I have followed the steps mentioned in the blog:

    http://blogs.msdn.com/b/exchangedev/archive/2015/01/21/building-demon-or-service-apps-with-office-365-mail-calendar-and-contacts-apis-oauth2-client-credential-flow.aspx

    After creating the account , I have created a self issued certificate and then uploaded the details in the manifest file.Once done , I am able to create the access token success fully.Once done , when I try to access the rest apis provided , I am getting the 401 error. I am using java to created the access token and then making the rest call. 

    I have posted this question on stack overflow , as below , to  which Mathias had cleared few querries. :

    http://stackoverflow.com/questions/30456100/getting-error-code-403-while-accessing-office-365-mail-box-for-the-client-creden

    Below is the code , that I am using to make the rest call :

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.UUID;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;

    import com.microsoft.aad.adal4j.AsymmetricKeyCredential;
    import com.microsoft.aad.adal4j.AuthenticationContext;
    import com.microsoft.aad.adal4j.AuthenticationResult;

    public class AccessToke {
    public static void main(String[] args) {
    String authority = "https://login.windows.net/tenant - id/oauth2/authorize";
        ExecutorService service = null;
        service=Executors.newFixedThreadPool(1);
        try {
            AuthenticationContext authenticationContext =  new AuthenticationContext(authority, false, service);
            String certfile = "E:\\Project\\Vantage\\oauth\\certs2\\pfx4.pfx";
            InputStream pkcs12Certificate=new FileInputStream(certfile);

            String token = "";
             
            
            AsymmetricKeyCredential credential = AsymmetricKeyCredential.create("clientid", pkcs12Certificate,"password");
            System.out.println("X509 is fine!");

            Future<AuthenticationResult> future=authenticationContext.acquireToken("https://outlook.office365.com", (AsymmetricKeyCredential)credential, null);
            System.out.println("Token Received "+future.get().getAccessToken());
            token=future.get().getAccessToken();
            System.out.println(token);
            Long uuid = UUID.randomUUID().getMostSignificantBits();
            
            //URL url = new URL("https://outlook.office365.com/api/v1.0/me/folders/inbox/messages?$count=true&$filter=isread%20eq%20false");
            URL url = new URL("https://outlook.office365.com/api/v1.0/emailaddress/folders/inbox/messages");
            HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
            con.setRequestMethod("GET"); 
            con.setRequestProperty("Accept","application/json"); 
            con.setRequestProperty("User-Agent","Testing/1.0 abc/1.1");
            Date date = new Date();
            System.out.println(date);
            SimpleDateFormat ft = 
                 new SimpleDateFormat ("E, dd MM yyyy hh:mm:ss zzz");

                 System.out.println("Current Date: " + ft.format(date));
                 String dateString = ft.format(date);
            con.setRequestProperty("Date:",dateString);
            con.setRequestProperty("client-request-id:",uuid+"");
            
            //con.setRequestProperty("Authorization",token);
            con.setRequestProperty("Authorization:","Bearer "+token);
            con.setRequestProperty("client_assertion:",token);
            System.out.println("Bearer "+token);
            
            if (con.getResponseCode() != 200) {
            System.out.println(con.getHeaderFields());
            //System.out.println(con.getRequestProperties());
    throw new RuntimeException("Failed : HTTP error code : "
    + con.getResponseCode());
    //System.out.println("Response from server" + con.getResponseCode());
    }
     
    BufferedReader br = new BufferedReader(new InputStreamReader(
    (con.getInputStream())));
     
    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
    System.out.println(output);
    }
     
    con.disconnect();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    }

    I was asked to check the x-ms-diagnostic header response for this request , however I am not getting any such header for the response.

    Also , I used a common url(like gmail/yahoo/etc) as my app id url , since I dont have any url of my own.Will this cause an issue?Also , when uploading the manifest file , with the keycredentials , the base64 , gets defaulted to null , even after uploading correctly.Is this behavior correct?

    Request to help . Also , if there is any sample code in java , request to provide .We are facing this issue , from quite sometime , however unable to make any progress.


    • Edited by AnupamNath Thursday, May 28, 2015 9:48 AM
    Thursday, May 28, 2015 9:46 AM

Answers

All replies