python-postnl-api icon indicating copy to clipboard operation
python-postnl-api copied to clipboard

UnauthorizedException is back

Open peter-mdf opened this issue 7 years ago • 34 comments

The UnauthorizedException is back like in the previous issue. Unclear is yet why, but apparently not everyone is suffering from this case, as noted in https://community.home-assistant.io/t/lovelace-postnl/112433/240

I tried to debug, but having a hard time to track my requests on an android device.

The earlier mentioned user agents here don't solve the problem. So it's unclear if it's related to the header.

cc @IcyPalm

peter-mdf avatar Nov 22 '19 16:11 peter-mdf

Yeah... PostNL made it a LOT harder. I appears that they are using: Akamai Bot Manager

Based on the X-acf-sensor-data header

Annotation 2019-11-23 210414

I'm not sure if/how we are able to bypass this.

IcyPalm avatar Nov 23 '19 20:11 IcyPalm

Damn... That sounds like they are actively blocking us....

peter-mdf avatar Nov 23 '19 20:11 peter-mdf

USPS had the same case: https://github.com/home-assistant/home-assistant/issues/13688

peter-mdf avatar Nov 23 '19 20:11 peter-mdf

Damn... That sounds like they are actively blocking us....

It's a possibility, if any PostNL devs are reading this: Let's have a beer and talk this over 🥂

IcyPalm avatar Nov 23 '19 20:11 IcyPalm

I wonder if we set the update time to once a day, if we are being "unblocked" after like a week or so. Since it took some time for some people to get blocked.

peter-mdf avatar Nov 23 '19 21:11 peter-mdf

I got around it.

Make the following changes in the code:

Change:

AUTHENTICATE_URL = BASE_URL + "/mobile/token"

To:

AUTHENTICATE_URL = BASE_URL + "/web/token"

Change:

"client_id": "pwAndroidApp",

To:

"client_id": "pwWebApp",

You are now using their web login, which still gives you an access key which also works with the mobile end points.

Potentially all end points could be rebuild to the web interface even....

peter-mdf avatar Nov 23 '19 22:11 peter-mdf

We have to check whether that is in the design specs of Homeassistant though, since it's borderline webscraping.

But I like the idea, I was just fiddling with the web api myself

IcyPalm avatar Nov 23 '19 22:11 IcyPalm

Well, it's still an API. So not really scraping an HTML page. On the other hand, PostNL didn't build a "bot protection" without reasons I suppose.

peter-mdf avatar Nov 23 '19 23:11 peter-mdf

Well I still have trouble to login. I changed the lines as you said, but still received an 403 http error :-(

D2R9 avatar Nov 24 '19 19:11 D2R9

I am working on a fix, currently trying out new ways to interface with the multiple endpoints

IcyPalm avatar Nov 24 '19 20:11 IcyPalm

Did anyone have contact with PostNL regarding the API implementation? Possibly we could start the discussion and also discuss items like refresh time with them.

iMicknl avatar Nov 25 '19 12:11 iMicknl

Tried through their webcare team, but failed. Was thinking about reaching out to them through Linkedin, so let me see what I can do.

peter-mdf avatar Nov 25 '19 13:11 peter-mdf

I got around it.

Make the following changes in the code:

Change:

AUTHENTICATE_URL = BASE_URL + "/mobile/token"

To:

AUTHENTICATE_URL = BASE_URL + "/web/token"

Change:

"client_id": "pwAndroidApp",

To:

"client_id": "pwWebApp",

You are now using their web login, which still gives you an access key which also works with the mobile end points.

Potentially all end points could be rebuild to the web interface even....

Works for me!

megapearl avatar Nov 25 '19 15:11 megapearl

I got around it. Make the following changes in the code: Change:

AUTHENTICATE_URL = BASE_URL + "/mobile/token"

To:

AUTHENTICATE_URL = BASE_URL + "/web/token"

Change:

"client_id": "pwAndroidApp",

To:

"client_id": "pwWebApp",

You are now using their web login, which still gives you an access key which also works with the mobile end points. Potentially all end points could be rebuild to the web interface even....

Works for me!

Not for me, still got 403 error.

D2R9 avatar Nov 25 '19 21:11 D2R9

I got around it. Make the following changes in the code: Change:

AUTHENTICATE_URL = BASE_URL + "/mobile/token"

To:

AUTHENTICATE_URL = BASE_URL + "/web/token"

Change:

"client_id": "pwAndroidApp",

To:

"client_id": "pwWebApp",

You are now using their web login, which still gives you an access key which also works with the mobile end points. Potentially all end points could be rebuild to the web interface even....

Works for me!

Does not work for me as well.

Muyz avatar Nov 26 '19 23:11 Muyz

Life got in the way, I had less time this week than expected. I probably won't have any time until sunday/monday to work on this with the attention it needs

IcyPalm avatar Nov 27 '19 23:11 IcyPalm

This works:

curl -i -s -k -X $'POST' \
    -H $'Host: jouw.postnl.nl' -H $'Accept: */*' -H $'Accept-Encoding: gzip, deflate' -H $'Accept-Language: nl' -H $'Content-Length: 100' -H $'Content-Type: application/x-www-form-urlencoded' \
    --data-binary $'\x0d\x0a\x0d\x0aclient_id=pwWebApp&grant_type=password&password=yoursecretpassword&username=some%40mailaccount.com' \
    $'https://jouw.postnl.nl/web/token'

So some headers are not necessary... Fix:

postnl_api.AUTHENTICATE_URL = "https://jouw.postnl.nl/web/token"
postnl_api.DEFAULT_HEADER = {"Api-Version":"4.18", "User-Agent": "PostNL/iOS/4.32.0"}

0x00-0xFF avatar Nov 28 '19 17:11 0x00-0xFF

I got around it.

Make the following changes in the code:

Change:

AUTHENTICATE_URL = BASE_URL + "/mobile/token"

To:

AUTHENTICATE_URL = BASE_URL + "/web/token"

Change:

"client_id": "pwAndroidApp",

To:

"client_id": "pwWebApp",

You are now using their web login, which still gives you an access key which also works with the mobile end points.

Potentially all end points could be rebuild to the web interface even....

Where (which file / code) do I need to change this?

sanderjonkers avatar Nov 29 '19 13:11 sanderjonkers

I tried the suggestions from above but with no luck.. it's still not working for me. I keep on getting;

_LOGGER.debug(f"Access denied. Failed to refresh, attempt {count} of {max}.")

I have opened a webcare ticket with postnl about 1.5 years ago with a request for a decent API or some form of IFTTT integration.. but they never got back to me. Everytime I ask them the ticket is still pending.

icepick3000 avatar Dec 04 '19 04:12 icepick3000

I've also set the scan_interval to max once a day for now. Not sure if that solves the issue, together with the above mentioned work-around.

peter-mdf avatar Dec 04 '19 10:12 peter-mdf