Google Alerts APIa - Authentication Issue Fix

in utopian-io •  6 months ago


image source

Repository

https://github.com/adasq/google-alerts-api
PR: https://github.com/adasq/google-alerts-api/pull/15

Bug Fixes

  • What was the issue(s)?

What is Google Alerts?

For those who are not familiar with Google Alerts - it is a simple service which lets you create kind of "Internet Watcher" for given search terms.

Some of the search term you can create a watcher for:

  • "STEEM OR SBD OR steemit"
  • "blockchain"
  • "Donald Trump"

more here: https://www.google.com/alerts

Background

If you think about it longer, it is a brilliant service which you can use widely. But it has a major drawback - Google does not provide a public API for it.

I really wanted to use the service in my private project, so I decided to create a NPM module ( google-alerts-api ) which lets you interact with the service via your javascript codebase.

A couple of people started to use it as well. And it was fine, until Google has changed the way we can authenticate pragmatically. It started to require CAPTCHA ( :/ ) in authenticate process. The package became useless because of inability to authenticate in order to manage client alerts.

Quiet a few users has noticed it as well ( see issue here ).

To make things even funnier, Google algorithms sometimes let you in w/o CAPTCHA, so there is no golden rule where the form with an image characters to re-type will appear.

  • What was the solution?

Well, I added support for CAPTCHA, so user ( while in an authentication process ) has to fill CAPTCHA. So, whenever you call api.sync ( and you are not authenticated yet ), you will be prompted with a message, requiring you to fill in CAPTCHA via command line:

A link ( https://accounts.google.com/Captcha?v=2&ctoken=... ) includes an image with CAPTCHA. You need to display it in a web browser, and re-type in the command line.

Finally, authentication process completes, so you can use a package to create / modify or remove your alerts.

But wait... do I have to re-type CAPTCHA each time I want to authenticate? It makes a package useless...

That is why another way to authenticate has been introduced some time ago. So called cookies based authentiaction. You can generate cookies:

and then use it (cookies string ) in this way:

You can store your cookies somewhere, and reuse it later, when you want to authenticate. Your username / password pair is no longer needed. Cookies is the only thing required to interact with an API.

How I did implement this:

First of all, while receiving a HTTP response with a HTML body, we have to find out, whether there is a CAPTCHA form available. A getCaptchaImageByBody function does the job:

Then, we have to find image url ( using getCaptchaImageByBody ) and ask client to re-type CAPTCHA:

A askUser function manages whole command-line stuff, its implementation is available here:

Finally, we can re-send a form with CAPTCHA included. And we are done!

A README.md has been also updated to give an API users brief summary, how does the thing work now:

GitHub Account

https://github.com/adasq

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for your contributions. It's indeed a nice fix for the user and very nicely explained. Where does the cookie get stored now, in the code itself?


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

·

Thank you for the review. According to the storing cookies. It is generated by a user and it is a simple string, so we can save it into a file, or store as an ENV variable in order to use it on our server side later on.

·

Thank you for your review, @codingdefined! Keep up the good work!

Hi, @adasq!

You just got a 0.42% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.


Congratulations @adasq!
You raised your level and are now a Minnow!

Do not miss the last post from @steemitboard:

The new Steemfest³ Award is ready!
Be ready for the next contest!

Support SteemitBoard's project! Vote for its witness and get one more award!

@adasq if you are in Poland, find me. Im in Krakow and would very much want to meet you. Or find me on Discord

Congratulations @adasq! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:

The Meet the Steemians Contest is over - Results are coming soon ...

Support SteemitBoard's project! Vote for its witness and get one more award!