Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

HTTP 403 error

Greetings,

 

I can run the Python code in Pycharm, there are no errors.

 

import urllib.request
with urllib.request.urlopen("URL/server/rest/services/CFA_Boundaries/FeatureServer/0/?start=0&limit=50&api_token=token") as url:
data = url.read().decode("utf-8")
output = data

 

I set the Data Source settings in Global permissions.

 

When I run it from within Power BI, using the Get Data > Other > Python Script.

I get a HTTP 403 error.

 

 

I would appreciate any assistance,

 

Regards,

 

Clive

  • Hi Anonymous 

    It might be that the user agent in PBI is being blocked, or the URL is somehow being malformed inside PBI?

    You could try using Fiddler to see what traffic is going between PBI and the website so you can see what PBI is sending.

    https://www.telerik.com/fiddler

    Phil

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Phil,

     

    Thanks for the suggestion. I ran the Python script in Pycharm, that worked.

    I have never used Fiddler, so wouldn't know how to link to Power BI??

     

    I followed Chris Webb's Blog (https://blog.crossjoin.co.uk/2014/03/26/working-with-web-services-in-power-query/), to minimise the number of times entering the API token.

    I have the Web.Contents() icon in Power BI Fields tag, but don't see the icon in the Data Sources Settings as shown in the Blog??

     

    When I run the Parse JSOn option, I get DataFormat.Error:

    DataFormat.Error: We found extra characters at the end of JSON input.
    Details:
    Value=
    Position=0

     

    Still can't run the REST URL??

     

     

     

  • v-yingjl's avatar
    v-yingjl
    5 years ago

    Hi Anonymous ,

    Have you checked the json format is valid before using it in power bi? Invalid json format could casue the data format error.

    About HTTP 403 error, you can refer these simliar issues and articles:

    1. 403 Error when trying to access REST API 
    2. Power BI REST API gives 403 error (User does not have access to group) 
    3. FIX: Power BI error Access to the resource is forbidden 

     

    In addition, since there is only data field in the table, could you please consider sharing some screenshots in power query about this issue?

     

    Best Regards,
    Yingjie Li

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

     

6 Replies

  • Hi Anonymous 

    It might be that the user agent in PBI is being blocked, or the URL is somehow being malformed inside PBI?

    You could try using Fiddler to see what traffic is going between PBI and the website so you can see what PBI is sending.

    https://www.telerik.com/fiddler

    Phil

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Phil,

       

      Thanks for the suggestion. I ran the Python script in Pycharm, that worked.

      I have never used Fiddler, so wouldn't know how to link to Power BI??

       

      I followed Chris Webb's Blog (https://blog.crossjoin.co.uk/2014/03/26/working-with-web-services-in-power-query/), to minimise the number of times entering the API token.

      I have the Web.Contents() icon in Power BI Fields tag, but don't see the icon in the Data Sources Settings as shown in the Blog??

       

      When I run the Parse JSOn option, I get DataFormat.Error:

      DataFormat.Error: We found extra characters at the end of JSON input.
      Details:
      Value=
      Position=0

       

      Still can't run the REST URL??

       

       

       

      • PhilipTreacy's avatar
        PhilipTreacy
        Super User

        Hi Anonymous 

        Fiddler is a proxy, it intercepts all HTTP(S) traffic between your PC and the internet.  You don't need to link/configure PBI to use it.  Once Fiddler is up and running it will see all web traffic for all programs so you just need to look for the PBI traffic to see what is being sent and received.

        That said, I am now confused by a few things in your reply.

        I was actually going to suggest you just use a regular Get Data -> Web query as all you Python script seems to be doing is fetching a web page.  Do you have to use Python for this?

        In your reply you mention Web.Contents(), so have you already switched to using this in PBI?

         

        I have the Web.Contents() icon in Power BI Fields tag, but don't see the icon in the Data Sources Settings as shown in the Blog??

        I'm not sure what exactly you are referring to here.  Chris Webb's blog post shows the Data Source Settings that are displayed the first time you connect to web content.  When you first run a web query this should pop up automatically.  So are you now using a web query?  If so can you please share it.

        Regarding the JSON error, is the query actually receiving JSON?

        Please share your query and the URL you are trying to reach.

        Phil


        If I answered your question please mark my post as the solution.

        If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.

  • Hi Anonymous 

    As v-yingjl points out, that JSON you posted isn't valid.  If that's what in the JSON file then that is what's causing the JSON DataFormat.Error.  You can check yourself here https://jsonlint.com/

    A valid version of what you posted is this

     

    {
    	"fields": [{
    			"name": "objectid",
    			"type": "esriFieldTypeOID",
    			"alias": "OBJECTID",
    			"domain": null,
    			"editable": false,
    			"nullable": false,
    			"defaultValue": null,
    			"modelName": "objectid"
    		},
    		{
    			"name": "cfa_id",
    			"type": "esriFieldTypeString",
    			"alias": "CFA_ID",
    			"domain": null,
    			"editable": true,
    			"nullable": true,
    			"length": 8000,
    			"defaultValue": null,
    			"modelName": "CFA_ID"
    		},
    		{
    			"name": "farm_holding_name",
    			"type": "esriFieldTypeString",
    			"alias": "Farm Holding Name",
    			"domain": null,
    			"editable": true,
    			"nullable": true,
    			"length": 8000,
    			"defaultValue": null,
    			"modelName": "Farm_Holding_Name"
    		}
    	]
    }

     

    Where's the JSON being generated?

    In my previous reply I asked if you really need to use Python as it appears all you are doing is getting data from a website so you could just use the Web connector in Power Query.

    A 403 error is the web server telling you that what you've tried to access is forbidden. I don't see how you can get a valid response in Python/PyCharm but running the same script in PBI gives a 403.

    Can you confirm that the URL you are trying to reach is exactly the same in both cases?

    Please share the query you are using in PBI.

    Phil


    If I answered your question please mark my post as the solution.

    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.