Pnpjs: Not able to make a request to the Graph API

Created on 14 May 2019  路  4Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [ 1.3.2 ]

Please specify what version(s) of SharePoint you are targeting: [ N/A ]

Expected / Desired Behavior / Question

I am trying to make a basic request to the Graph API to get the list of groups that exist in a tenant

But I get this error
UnhandledPromiseRejectionWarning: Error: Get Token request returned http error: 400 and server response: {"error":"invalid_request","error_description":"AADSTS90002: Tenant '' not found. This may happen if there are no active subscriptions for the tenant. Check with your subscription administrator.\r\nTrace ID: 2f0a063b-344e-4b08-94ee-7e600cfbae00\r\nCorrelation ID: 9a44bdd0-fa06-49c0-97b2-2a2644fbe575\r\nTimestamp: 2019-05-14 17:29:26Z","error_codes":[90002],"timestamp":"2019-05-14 17:29:26Z","trace_id":"2f0a063b-344e-4b08-94ee-7e600cfbae00",

This is the code I have written-

import { graph } from "@pnp/graph";
import { AdalFetchClient } from "@pnp/nodejs";

graph.setup({
  graph: {
      fetchClientFactory: () => {
          return new AdalFetchClient( "{Client ID}",
          "domain.onmicrosoft.com",
          "");
      },
  },
});

// make a call to Graph and get all the groups
graph.groups.get().then(g => {
  console.log(JSON.stringify(g, null, 4));
});

I registered an app in Azure using the Steps listed here -
https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app

Any help on this matter would be greatly appreciated!

Observed Behavior

It should be able to give me the list of groups that exist in the tenant.

Steps to Reproduce

Registerer an app in Azure using the Steps listed here -
https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app

import { graph } from "@pnp/graph";
import { AdalFetchClient } from "@pnp/nodejs";

graph.setup({
  graph: {
      fetchClientFactory: () => {
          return new AdalFetchClient( "{Client ID}",
          "domain.onmicrosoft.com",
          "");
      },
  },
});

// make a call to Graph and get all the groups
graph.groups.get().then(g => {
  console.log(JSON.stringify(g, null, 4));
});

Submission Guidelines

Delete this section after reading

  • All suggestions, questions and issues are welcome, please let us know what's on your mind.
  • Remember to include sufficient details and context.
  • Please check back occasionally on your issue as we may have follow up questions.
  • If you have multiple suggestions, questions, or bugs please submit them in seperate issues so we can track resolution.

Thank you for your feedback!

code not a bug question

Most helpful comment

Just to add to this:
A deployed SPFx Graph component needs to be granted it's API Permissions from the modern Admin Centre.

If you head to the modern SharePoint Admin Page theres a section called 'API Management' and you can approve the application from there. Installing should have requested these permissions. If testing from a local workbench it uses your user permissions for the Graph authentication.

All 4 comments

Hi @rajivsr23,

Looks that parameters provided to AdalFetchClient are in the wrong order, should be: AdalFetchClient("{tenant}.onmicrosoft.com", "{app id}", "{app secret}").

That's why {tenant} ends up with an empty string and clientId/clientSecret pair incorrect.

Hi @koltyakov ,

Thanks for your reply!

I have modified ADALFetchClient, but now I get a different error

(node:18696) UnhandledPromiseRejectionWarning: Error: Error making HttpClient request in queryable [403] Forbidden ::> {
  "error": {
    "code": "Authorization_RequestDenied",
    "message": "Insufficient privileges to complete the operation.",
    "innerError": {
      "request-id": "624769b0-224c-4b22-9d6c-8d155e089c07",
      "date": "2019-05-14T20:37:10"
    }
  }
}

The good news as it returns 403 it authenticates. The error is self-descriptive, what can be said more, the app registration is not granted with corresponding permissions. 馃槈 That's the reason.

Just to add to this:
A deployed SPFx Graph component needs to be granted it's API Permissions from the modern Admin Centre.

If you head to the modern SharePoint Admin Page theres a section called 'API Management' and you can approve the application from there. Installing should have requested these permissions. If testing from a local workbench it uses your user permissions for the Graph authentication.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simkessy picture simkessy  路  3Comments

AJIXuMuK picture AJIXuMuK  路  3Comments

simonagren picture simonagren  路  3Comments

un-dres picture un-dres  路  3Comments

AJIXuMuK picture AJIXuMuK  路  3Comments