Sunday, January 13, 2019

Python3: Authenticate with Discord API using urllib.request

I wanted to do some automation with Discord, so I started writing a Python script to query Discord's API. First step is to authenticate with Discord. Following the documentation, I sent a POST request to the token URL. However, I was getting a HTTP 403 with error message
"Please enable cookies. Error 1010...Access denied...The owner of this website (discordapp.com) has banned your access based on your browser's signature..." It appears to be protected by Cloudflare.

First thing I tried was to use http.cookiejar for my POST request, but that did not help. Following the sample code in the documentation, I switched over to requests instead of the built-in library urllib.request, and it worked! But I was curious why it did not work with urllib.request.

Then I tried Postman to send the POST request, and it worked as well. Postman has a code generation feature for http.client (Python 3). There were some errors in the generated code, but I was able to fix them and got it to work with Discord. http.client is a low-level library and used by urllib.request, so why is it working with http.client but not urllib.request?

Next, I fired up Wireshark to monitor and compare the POST requests. After bunch of trial and error, I found the answer to be rather simple.

User-Agent:
"" (empty string) - works
"python-requests/2.21.0" - works
"Python-urllib/3.6" - does not work

It turns out Cloudflare does not like the user-agent string from urllib.request. Requests from http.client do not have an user-agent set by default, thus it worked. So for anyone who want to use urllib.request, set the user-agent to blank or match with your browser should avoid this error.

Thursday, December 27, 2018

Win10: Look up build and version info

I was trying to find out if I'm running Windows 10 1809. I checked Control Panel > System (Win+Pause) but it's not listed there.

Instead, open the Settings app (Win+I), then navigate to System >  About. The OS build and version is listed under Windows specifications

Ref:
How to Find Out Which Build and Version of Windows 10 You Have

Friday, November 9, 2018

Win10: Photos app opens extremely slow

The Photos app on Windows 10 is just painfully slow.

Solution:
1. go to Settings > Privacy
2. under App permissions on the left, scroll down to Pictures
3. turn OFF Allow apps to access your pictures library
---
4. go to App & features
5. select Microsoft Photos > Advanced options > Reset
---
6. open Photos
7. click on the overflow menu (three dots) > Settings
8. remove any folders from collection
9. turn OFF Linked duplicates
10. turn OFF People

Alternatively, you can change the default program back to Windows Photo Viewer.

Sunday, July 8, 2018

Alexa: AWS regions for Lambda functions

Playing around with Alexa Skills Kit. I was having an issue where my lambda function was not being triggered by Alexa. The lambda function itself works fine when testing within AWS Console. The issue turns out related to the region where my lambda function was created.

For custom skills, Lambda functions can be hosted in one of these regions:
  • Asia Pacific (Tokyo)
  • EU (Ireland)
  • US East (N. Virginia)
  • US West (Oregon)

For smart home skill:
  • N.Virginia for English (US) or English (CA) skills
  • EU (Ireland) region for English (UK), English (IN), German or French (FR) skills
  • US West (Oregon) for Japanese and English (AU) skills.

Friday, June 8, 2018

Firefox: getting user-agent

I used to get my Firefox user-agent string through about: But in the recent version of Firefox, about: is no longer available. Instead, the user-agent can now be found under about:support

Ref:
Page "about:" removed in Firefox 59? | Firefox Support Forum | Mozilla Support

Tuesday, June 5, 2018

Kindle E-reader: disable screensaver

* only tested with a Kindle Keyboard

The power button of my Kindle Keyboard stopped working all the sudden. When I toggle it, it doesn't go into sleep mode. After leaving it idle for awhile, it went into sleep mode automatically. However, I can't turn it back on!!

Interestingly, the power button is not completely broken. I managed to reset the device by holding the power button for 30 seconds. Once it finish reboot, everything (except the power button) continue to work. As a workaround, I disabled the screensaver so I don't need to turn it ON/OFF again.

1. press Home
2. press ↵ (the enter key)
3. a search bar would appear
4. enter ;debugOn
5. enter ~disableScreensaver
6. enter ;debugOff

If you change your mind, replace step 5 with the following command:

~resumeScreensaver

Ref:
How To Turn Off The Kindle Screensaver – Permanently | IT Support Forum