2

I'm getting 403 forbidden error in my SPFx web part when trying to access Teams presence (mine and other users as well). I have set permissions in package-solution:

Permissions:

  {
    "resource": "Microsoft Graph",
    "scope": "Presence.Read"  
  },
  {
    "resource": "Microsoft Graph",
    "scope": "Presence.Read.All" 
  }

Call for presence:

this.context.msGraphClientFactory
.getClient()
.then((client: MSGraphClient) => {

  client
    .api(`users/{username}/presence`)
    .version("beta")
    .get((err, res) => {  
      if (err) {
        alert(JSON.stringify(err));
        return;
      }
      console.log(res);         
    });
});

Any idea what is wrong please?

2 Answers 2

0

You can also get the user's presence using MSGraphClient in SPFx.

Get the Graph Client using:

import { MSGraphClient } from '@microsoft/sp-http';
...

private _graphHttpClient: MSGraphClient;

return new Promise((resolve, reject) => {
      this.context.msGraphClientFactory.getClient().then(client => {
        this._graphHttpClient = client;
        resolve();
      }).catch(error => {
        console.log(error);
        reject(error);
      });
    });

Then you can get the presence of user using:

const endpoint: string = `https://graph.microsoft.com/beta/communications/getPresencesByUserId`;
const response: any = await this.props.graphHttpClient.api(endpoint).post({
    "ids": users  //Here you need to pass the user ID's
});
const graphResponse: any = response.value;

Check the complete code at GitHub SPFx webpart sample:

Reference: react-members-with-presence.

4
  • Thanks, but it's still the same :( . I get: POST https://graph.microsoft.com/beta/communications/getPresencesByUserId 403 (Forbidden). Is getting presence even available already? It's still beta. I have seen notify like that: "Use of these APIs in production applications is not supported." here: link
    – Vochomurka
    Mar 2, 2020 at 11:03
  • Yes, it is in beta and not supported for production environments. If you are planning to use this in production environment then I will suggest you to don't use it. Also, are you tenant administrator? Mar 2, 2020 at 12:49
  • Not tenant, but I'm SharePoint admin. What is meant by "production enviroment"? Are there any other uses of getting presence then in public apps? Is it only for testing so far when we can't use this publicly? It's little bit confusing for me.
    – Vochomurka
    Mar 2, 2020 at 12:52
  • Oct 2021 and it's still in Beta. Come on Microsoft - what's the point in including things that never get released? Oct 14, 2021 at 13:41
0

You need to approve the permissions in your SharePoint admin center first before you can make Graph requests, and each Graph permission you need will require approval.

Create a package to upload to your Apps library (it doesn't matter that it isn't ready for deployment) and then after you've done that, go to SharePoint admin and go to the API Access under advanced, then look for your Graph permissions and approve them. I shared an image of my admin with permission waiting for approval

Now you can get back to building out your spfx and testing the web calls without getting 403 :)

edit Your spfx package must have the permissions you require in the package-solution.json before you upload it to the Apps library

SPO admin view

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.