Postman To Graph API

1 minute read

Description:

So in my previous post I talked about using Postman to run pre-request scripts so that it can get an Oauth Token and attach it to each request. Here we are going to do the same thing but instead we will connect to Microsoft Graph API. This assumes that you created an application before (although will work for user as well (see below):

To Resolve:

  1. First you will need to replace some-tenant-id, client_id, client_secret, username, and password in the examples to come - make sure you have them. Go into your connection, and edit the pre-request script to be like:

    • To connect as an application:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    const echoPostRequest = {
       url: 'https://login.microsoftonline.com/some-tenant-id/oauth2/v2.0/token',
       method: 'POST',
       header: {
          'Accept': 'application/json',
          'Content-Type': 'application/x-www-form-urlencoded'
       },
       body: {
          mode: 'urlencoded',
          urlencoded: [
             { key: "grant_type", value: "client_credentials" },
             { key: "scope", value: "https://graph.microsoft.com/.default" },
             { key: "client_id", value: "mySuperLongClientID" },
             { key: "client_secret", value: "mySuperLongClientSecret" }
          ]
       }
    };
    
    pm.sendRequest(echoPostRequest, function (err, response) {
       console.log(response.json());
       var responseJson = response.json();
       pm.environment.set('currentAccessToken', responseJson.access_token)
       pm.environment.set('currentRefreshToken', responseJson.refresh_token)
    });
    
    • To connect as as a user:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    const echoPostRequest = {
       url: 'https://login.microsoftonline.com/some-tenant-id/oauth2/v2.0/token',
       method: 'POST',
       header: {
          'Accept': 'application/json',
          'Content-Type': 'application/x-www-form-urlencoded'
       },
       body: {
          mode: 'urlencoded',
          urlencoded: [
             { key: "grant_type", value: "Password" },
             { key: "scope", value: "https://graph.microsoft.com/.default" },
             { key: "client_id", value: "mySuperLongClientID" },
             { key: "client_secret", value: "mySuperLongClientSecret" },
             { key: "Username", value: "myUser@company.com" },
             { key: "Password", value: "seeKeePass" }
          ]
       }
    };
    
    pm.sendRequest(echoPostRequest, function (err, response) {
       console.log(response.json());
       var responseJson = response.json();
       pm.environment.set('currentAccessToken', responseJson.access_token)
       pm.environment.set('currentRefreshToken', responseJson.refresh_token)
    });
    
  2. Inside the connection just set Authorization tab to:
    • type: Bearer Token
    • Value: ``
  3. For requests that don’t use application/x-www-form-urlencoded (untested):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    header: {
       'Accept': 'application/json',
       'Content-Type': 'application/json'
    },
    body: {
       mode: 'raw',
       raw: JSON.stringify({ 
          grant_type : "client_credentials"
          scope : "https://graph.microsoft.com/.default"
          client_id : "seeKeePass"
          client_secret : "seeKeePass" 
          })
    }
    
    • If that doesn’t work, check this Gist for other options

-----------------------------------------------------------
Spotted a mistake in this article? Why not suggest an edit!
Did you like the article? Donations are always welcome!
-----------------------------------------------------------

Comments