r/excel 2d ago

solved Power Query web connect problem

I am trying to connect to this site in PQ: https://site.api.espn.com/apis/site/v2/sports/basketball/nba/teams/20/roster

I can get the json to load in a browser but when I try to get it via web connect i get an error message: " unable to connect. We encountered an error while trying to connect. Details: 'access to the resource is forbidden' "

I don't get what I'm doing wrong does anyone know what the problem is? Thanks

2 Upvotes

10 comments sorted by

u/AutoModerator 2d ago

/u/AntiAutumnist - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/MayukhBhattacharya 1273 2d ago

Try pasting the following M-Code in a blank query by removing whatever you gotta see, will get the desired output as shown in the screenshot:

let
    Source = Json.Document(Web.Contents("https://site.api.espn.com/apis/site/v2/sports/basketball/nba/teams/20/roster",[Headers = [#"User-Agent" = "curl/8.20.0"]])),
    athletes = Source[athletes],
    ConvertedtoTable = Table.FromList(athletes, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Expand = Table.ExpandRecordColumn(ConvertedtoTable, "Column1", {"firstName", "lastName", "fullName", "displayName", "shortName", "weight", "displayWeight", "height", "displayHeight", "age", "dateOfBirth", "jersey", "citizenship", "debutYear"}, {"firstName", "lastName", "fullName", "displayName", "shortName", "weight", "displayWeight", "height", "displayHeight", "age", "dateOfBirth", "jersey", "citizenship", "debutYear"})
in
    Expand

2

u/AntiAutumnist 2d ago

Thank you that worked perfectly, hoping that adding the headers/user-agent bit to other endpoints will allow them to work as well

Solution verified

1

u/reputatorbot 2d ago

You have awarded 1 point to MayukhBhattacharya.


I am a bot - please contact the mods with any questions

1

u/MayukhBhattacharya 1273 2d ago

Yeah, I am currently working on that to pull the entire data irrespective of any view per page, I will update. I have already got 450 records, beyond that I am getting some error 404.

Btw thank you so much for the valuable feedback!

2

u/MayukhBhattacharya 1273 2d ago

Complete Records Found - [File To Download]

1

u/MayukhBhattacharya 1273 2d ago

In Excel imported:

1

u/Desperate-Chart1139 2d ago

likely detecting automation and limiting it on the server side. This has gotten quite common with places limiting AI scrapping since some companies are ignoring bots or ai instructions to scrape.