Sharepoint framework and Graph API

%3CLINGO-SUB%20id%3D%22lingo-sub-14505%22%20slang%3D%22en-US%22%3ESharepoint%20framework%20and%20Graph%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-14505%22%20slang%3D%22en-US%22%3E%3CP%3EBased%20on%20the%20existing%20tutorials%20I%20tried%20to%26nbsp%3Badd%20a%20call%20to%20the%20graph%20api%20to%20see%20if%20it%20would%20work%2C%20based%20on%20the%20principle%20that%20by%20running%20the%20spx%20web%20part%20within%20sharepoint%20%26nbsp%3BI%20have%20already%20authenticated.%20Ie%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Ereturn%20this.context.httpClient.get(%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fmessages%22)%3C%2FPRE%3E%3CP%3Eetc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGiven%20they%20are%20different%20end%20points%20I%20wasn't%20that%20supprised%20It%20didn't%20work%20due%20to%20authentication%20failure%20so%20what%20would%20I%20need%20to%20do%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-14505%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-16455%22%20slang%3D%22en-US%22%3ERE%3A%20Sharepoint%20framework%20%20and%20Graph%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-16455%22%20slang%3D%22en-US%22%3EIn%20BRK4016%20they%20mentioned%20that%20it%20is%20not%20possible%20right%20now%20to%20call%20the%20Graph%20from%20SpFX%20because%20you%20can't%20get%20to%20the%20bearer%20token.%20But%20it%20seemed%20they%20were%20working%20on%20fix%20for%20that.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-16311%22%20slang%3D%22en-US%22%3ERe%3A%20Sharepoint%20framework%20%20and%20Graph%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-16311%22%20slang%3D%22en-US%22%3E%3CP%3EFrom%20all%20that%20I've%20read%2Fseen%20so%20far%2C%20it%20is%20not%20yet%20possible%20to%20call%20the%20Microsoft%20Graph%20API%20directly%20from%20an%20SPFx%20web%20part.%20I%20assume%20it%20is%20something%20that%20is%20being%20worked%20on%20and%20potentially%20even%20being%20addressed%20at%20Ignite.%20However%2C%20just%20so%20I%20could%20play%20with%20some%20ideas%20in%20anticipation%20of%20that%2C%20I%20followed%20the%20guidance%20in%20this%20%3CA%20href%3D%22http%3A%2F%2Fwww.chrisjohnson.io%2F2015%2F11%2F06%2Fsimplifying-office-365-unified-api-calls-with-postman-and-oauth-2%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eblog%20post%20by%20Chris%20Johson%3C%2FA%3E%26nbsp%3Bto%20grab%20an%20auth%20token%20using%20PostMan%26nbsp%3Bto%20submit%20with%20my%20httpClient.get%20request.%20You%20have%20to%20implement%20the%26nbsp%3BIHttpOptions%20interface%20and%20add%20an%20authorization%20bearer%20token.%20It's%20obviously%20not%20a%20realistic%20long-term%20approach%20but%20definitely%20something%20that%20you%20can%20leverage%20for%20now%20while%20the%20SPFx%20is%20still%20in%20preview.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20hint%3A%20I%20had%20to%20use%20the%20actual%20Chrome%20PostMan%20plug-in.%20For%20some%20reason%2C%20the%20stand-alone%20Windows%20client%20would%20not%20render%20my%20O365%20login%20screen%20correctly.%20Seemed%20like%20something%20to%20do%20with%20how%20the%20PostMan%20client%20called%20the%20Chrome%20rendering%20engine%20or%20something.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

Based on the existing tutorials I tried to add a call to the graph api to see if it would work, based on the principle that by running the spx web part within sharepoint  I have already authenticated. Ie,

 

return this.context.httpClient.get("https://graph.microsoft.com/v1.0/me/messages")

etc.

 

Given they are different end points I wasn't that supprised It didn't work due to authentication failure so what would I need to do?

2 Replies
Highlighted

From all that I've read/seen so far, it is not yet possible to call the Microsoft Graph API directly from an SPFx web part. I assume it is something that is being worked on and potentially even being addressed at Ignite. However, just so I could play with some ideas in anticipation of that, I followed the guidance in this blog post by Chris Johson to grab an auth token using PostMan to submit with my httpClient.get request. You have to implement the IHttpOptions interface and add an authorization bearer token. It's obviously not a realistic long-term approach but definitely something that you can leverage for now while the SPFx is still in preview.

 

One hint: I had to use the actual Chrome PostMan plug-in. For some reason, the stand-alone Windows client would not render my O365 login screen correctly. Seemed like something to do with how the PostMan client called the Chrome rendering engine or something.

Highlighted
In BRK4016 they mentioned that it is not possible right now to call the Graph from SpFX because you can't get to the bearer token. But it seemed they were working on fix for that.