Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
OneWithQuestion
Post Prodigy
Post Prodigy

Web.Contents throws error when trying to pass token: Credentials are required to connect to the Web

Credentials are required to connect to the Web source. You'll need to refresh and provide credentials to continue. (Source at https://login.microsoftonline.com/***************************/oauth2/v2.0/token.)

 

I was following the guide to use Graph API with Power BI and everything was working fine up until this step where I obtain a token and go to pass it via M in PowerBI Desktop https://docs.microsoft.com/en-us/power-query/samples/mygraph/readme#implementing-the-oauth-interface

 

I am unsure how to get past this issue, as I can't resolve the error.

 

I go to login with Anonymous as instructed, but it just throws the error after that:

anon login.png

 

 

 

Here is the M query I'm using, maybe I'm making a mistake there?

 

 

-------------

let
client_id = "**myclientID***",
redirect_uri = "https://login.microsoftonline.com/common/oauth2/nativeclient",
token_uri = "https://login.microsoftonline.com/*****tenant*****/oauth2/v2.0/token",
authorize_uri = "https://login.microsoftonline.com/****tenant*****/oauth2/v2.0/authorize",
logout_uri = "https://login.microsoftonline.com/logout.srf",

scope_prefix = "https://graph.microsoft.com/",
scopes = {
"User.Read.All",
"Directory.Read.All",
"Organization.Read.All"
},

Value.IfNull = (a, b) => if a <> null then a else b,

GetScopeString = (scopes as list, optional scopePrefix as text) as text =>
let
prefix = Value.IfNull(scopePrefix, ""),
addPrefix = List.Transform(scopes, each prefix & _),
asText = Text.Combine(addPrefix, " ")
in
asText,

authorizeUrl = authorize_uri & "?" & Uri.BuildQueryString([
client_id = client_id,
redirect_uri = redirect_uri,
scope = GetScopeString(scopes, scope_prefix),
response_type = "code",
response_mode = "query",
login = "login"
]),


tokenResponse = Web.Contents(token_uri, [
Content = Text.ToBinary(Uri.BuildQueryString([
client_id = client_id,
code = "****token I get back manually****",
scope = GetScopeString(scopes, scope_prefix),
grant_type = "authorization_code",
redirect_uri = redirect_uri])),
Headers = [
#"Content-type" = "application/x-www-form-urlencoded",
#"Accept" = "application/json"
],
ManualStatusHandling = {400}
]),
body = Json.Document(tokenResponse),
result = if (Record.HasFields(body, {"error", "error_description"})) then
error Error.Record(body[error], body[error_description], body)
else
body
in

result

 

******************

 

I think I'm failing here in red:

oauth_failure_pt.png

2 ACCEPTED SOLUTIONS

Basically everything has to go into the custom connector.

Easiest would probably be to use the one from the GitHub repo and expand further as needed.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

No, as I tried to express through the title of my article: "How to get more out of your Graph API custom connector in Power BI",

it just covers some special cases where the standard-connector from GitHub falls short 😉

 

The standard GitHub-connector returns a lot of good stuff already.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

5 REPLIES 5
ImkeF
Super User
Super User

Hi you cannot run this as a simple query, but have to use it via a custom connector: https://github.com/Microsoft/DataConnectors/tree/master/samples/MyGraph

 

some additional tips on how to use it here: https://www.thebiccountant.com/2019/04/17/graph-api-custom-connector-power-bi/

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Do you have an example of how it should be split up by chance?  Or could you post a screen shot?

 

I'm not clear what parts stay in PowerBI and what parts will only exist in the custom connector.

Basically everything has to go into the custom connector.

Easiest would probably be to use the one from the GitHub repo and expand further as needed.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Ah, interesting.  I thought it would require some parts to stay in PBI as M or whatnot.

 

So really, I can just compile the customer connector from GitHub and then use it in PBI and start consuming data?  I don't have to use the example article's M code?

No, as I tried to express through the title of my article: "How to get more out of your Graph API custom connector in Power BI",

it just covers some special cases where the standard-connector from GitHub falls short 😉

 

The standard GitHub-connector returns a lot of good stuff already.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors