puppeteer: "Couldn't sign you in" Google account login fails in headless mode in Google Cloud Functions

Hi everyone,

I’m trying to make a script that would login to Medium to export article’s stats. Medium allows Google authentication so I’m using that. It runs without any issue on my laptop (MacOS) but I get the message (attached to this issue) that I couldn’t sign in when run in Google Cloud Functions. Basically this login failure message appears after the screen where you enter your username.

  • Puppeteer version: 1.18.1 (also tested with 1.19.0, same result)
  • Platform / OS version: Google Cloud Functions
  • Node.js version: 8

What steps will reproduce the problem?

I deployed the code below to Google Cloud Functions.

exports.exportMediumStats = (req, res) => {
  const puppeteer = require('puppeteer');
  const url = "https://medium.com/m/signin?redirect=https%3A%2F%2Fmedium.com%2F&operation=login";
  let image;

  // Main function that will scrap Medium stats page using puppeteer
  async function main() {
    const browser = await puppeteer.launch({
      headless: true,
      args: ['--no-sandbox', '--disable-setuid-sandbox'],
    });
    const page = await browser.newPage();
    await page.goto(url, { waitUntil: "networkidle2" });
    console.log('Clicking on google login...');
    await page.click('.button--withChrome.button--large');
    await page.mainFrame().waitForSelector('#identifierId');
    console.log('Typing email...');
    await page.type('#identifierId', process.env.MEDIUM_USERNAME);
    await page.mainFrame().waitForSelector('#identifierNext');
    console.log('Clicking next button...');
    await page.click('#identifierNext');
    await page.waitFor(3000);
    image = await page.screenshot(); 

    console.log('Closing browser...');
    await browser.close();
  }

  main()
  .then(url => {
    res.setHeader("content-type", "image/jpeg");
    res.status(200).send(image);
  })
  .catch(err => {
    console.error(err);
    res.status(500).send("An Error occured" + err);  
  })
};

What is the expected result? I should get the screen where I would type my password. I works perfectly on my local machine in both non-headless and headless modes. Also it used to work on Google Cloud Functions at the beginning but it doesn’t for some time now.

What happens instead? The login fails with a message displayed in the attached picture below. The failure message is:

Couldn't sign you in
For your protection, you can't sign in from this device.
Try again later, or sign in from another device.

couldnt_sign_in

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 20
  • Comments: 21

Most upvoted comments

Hey. I was able to solve the problem!

I am running my script on Digital Ocean Droplet with Ubuntu 18

  1. I followed this instruction https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth

  2. Then I installed xvfb https://www.npmjs.com/package/xvfb to run puppeteer in headfull mode

Then the most important. Without these steps, Google will still block you.

  1. I installed chrome instance via command line

  2. Launch browser with chrome-launcher package

Code example


        const puppeteer = require("puppeteer-extra")
        const pluginStealth = require("puppeteer-extra-plugin-stealth")
        puppeteer.use(pluginStealth())
        const chromeLauncher = require('chrome-launcher');
        const axios = require('axios');
        const Xvfb = require('xvfb');

        const xvfb = new Xvfb();
        xvfb.startSync();
        const chromeConfig = {
                chromePath: "/usr/bin/google-chrome-stable"
        }

        const chrome = await chromeLauncher.launch(chromeConfig);
        const response = await axios.get(`http://localhost:${chrome.port}/json/version`);
        const { webSocketDebuggerUrl } = response.data;
        const browser = await puppeteer.connect({ browserWSEndpoint: webSocketDebuggerUrl });
        
        //log in into google account....

Good luck! 😃

Hi! I have got the same problem. I run puppeteer in the non-headless mode and also implemented the recommendation from this article https://intoli.com/blog/making-chrome-headless-undetectable/ After all the steps, I still receive this message.

Is anyone have a solution to this problem? I would really appreciate any help.

Have a nice day 😃

who can fix ?

@ksimir I’d imagine that this due to some anti-bot behaviour on Google’s end. Try altering the User-Agent header.

I’ve hit this problem previously. It appears to be some sort of bot detection.

What worked for me was not setting the userAgent, and running non-headless. Either spoofing the userAgent or running headless would cause me to hit this issue.

Best of luck.

Hi, How did you avoid the (presumably) IP whitelisting? My issue is that when I test in local everything works but when I move the program to production environment it tries to send me an email with a code even though I haven’t got 2 factor auth activated