0

I am already aware to call microsft graph API using postman and code base for v1.0 . Question : How to call microsoft graph beta api via postman and codebase using java for beta version.

There are lots of articles related to v1.0 but i do not find any related to beta version. i think, it might be possible that we can call beta version API using custom API call. Please suggest or share any article.

Note: I know it is not recommended to use beta version API but I am working on some POC to test this

Ashish Goyanka
  • 207
  • 3
  • 11

1 Answers1

0

I'm not sure if I misunderstand you.

Here's a format of graph api, and you could manage {version} to switch 'beta' version and 'v1.0' version. Here's the description.

{HTTP method} https://graph.microsoft.com/{version}/{resource}?{query-parameters}

You can change the input choice to get different versions of one api. enter image description here enter image description here

=============UPDATE============

//js sample call graph api
function callMSGraph(endpoint, token, callback) {
    const headers = new Headers();
    const bearer = `Bearer ${token}`;
    headers.append("Authorization", bearer);    
    const options = {
        method: "GET",
        headers: headers
    };
    fetch(endpoint, options)
        .then(response => response.json())
        .then(response => callback(response, endpoint))
        .catch(error => console.log(error))
}

java sample call graph api, code comes from this

private String getUserInfoFromGraph(String accessToken) throws Exception {
        // Microsoft Graph user endpoint
        URL url = new URL(authHelper.getMsGraphEndpointHost() + "v1.0/me");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // Set the appropriate header fields in the request header.
        conn.setRequestProperty("Authorization", "Bearer " + accessToken);
        conn.setRequestProperty("Accept", "application/json");

        String response = HttpClientHelper.getResponseStringFromConn(conn);

        int responseCode = conn.getResponseCode();
        if(responseCode != HttpURLConnection.HTTP_OK) {
            throw new IOException(response);
        }

        JSONObject responseObject = HttpClientHelper.processResponse(responseCode, response);
        return responseObject.toString();
    }
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
  • Thanks for your suggestions. I also want to call beta api using microsft graph but i see...Support is not available in sdk to call beta API.is there any way using custom request or http client ? – Ashish Goyanka Feb 03 '21 at 08:34
  • Do you mind telling me which api you would like to call? – Tiny Wang Feb 03 '21 at 08:42
  • its get and update policy API( Microsoft graph API )- https://learn.microsoft.com/en-us/graph/api/trustframeworkpolicy-get?view=graph-rest-beta – Ashish Goyanka Feb 04 '21 at 09:13
  • Oh I see what you mean 'not support', don't worry man, this tip is meant to tell the users that beta version apis are in preview so that they are not stable. And I've tried use [this api](https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-beta&tabs=http) in my spa, and it worked well. Or I misunderstood sth? Pls point it out thanks. – Tiny Wang Feb 04 '21 at 09:34
  • Thanks for your response. Could you please try API to get policy - GET https://graph.microsoft.com/beta/trustFramework/policies/B2C_1A_Test/$value – Ashish Goyanka Feb 04 '21 at 09:41
  • Sorry, as my directory is not b2c, so when I call the api I got the error of ' is not an Azure AD B2C directory. Access to this Api can only be made for an Azure AD B2C directory.'. And according to the error message you provided, you need to make sure you has set the 'Authorzation' header. See my answer for my http request. – Tiny Wang Feb 04 '21 at 09:53
  • Thanks.. are you trying this with any tool (postman etc) or using code (java etc)...please share code if you are trying using code (java etc) – Ashish Goyanka Feb 04 '21 at 11:45
  • I answered a question on how to [use graph api to access key vault](https://stackoverflow.com/questions/65747678/how-access-a-secret-from-keyvault/65763064#65763064) with javascript, it's similar with yours, just change the scope of calling api and api endpoint. And if you wanna a sample in java, I think [this](http://xqting.com/msgraphandmsalinjavaformsteams) can help you. – Tiny Wang Feb 04 '21 at 13:53
  • @AshishGoyanka Hi, do you have any progress? – Tiny Wang Feb 08 '21 at 02:01