Search code examples
google-oauthgoogle-account

Determining the "u" part of a Google Thumbnail URL


Using Google Apps Script, I generate a list of users and their photo thumbnails for a work intranet. This intranet requires logging into Google using Oauth.

The thumbnail URLs look something like:

https://lh3.google.com/ao/AHP4FtlN1wzP_pjUvk0MEP-2tSZumfJi9eMJmCvi0mK8QDvuUSyU-kzXARP_oCrxjC6ugchgVUA=s288-c

This on its own will not work and yields the following "generic" silhouette placeholder:

Siloutte planceholder

If I add the following "user" part to the URL: /u/1

https://lh3.google.com/u/1/ao/AHP4FtlN1wzP_pjUvk0MEP-2tSZumfJi9eMJmCvi0mK8QDvuUSyU-kzXARP_oCrxjC6ugchgVUA=s288-c

The image works and I get the correct thumbnail at the requested size (288x288 pixels )

I understand that the 1 in /u/1 denotes the order I've logged into my Google account. One means it's the second account I've logged into, 0 being the first.

I've also found that for some Google URLs, I can replace the number with the account I want to use to access the resource.

e.g.

Google Drive thumbnails:

https://drive.google.com/thumbnail?authuser=lee.taylor@example.com&sz=w175&id=ALV-UjXcpIrWPSs-S6EKh2joeQh_C18zIGCcCqBt9arhmKbF1_kaZZp

Google Calendar links: https://calendar.google.com/calendar/b/lee.taylor@example.com/r/eventedit/1234567890

So, my question is. How can I determine the user number that my email address maps to?

I am using PHP with GoogleOAuth to login with the following code/scopes:

$this->client = new Google\Client();
$this->client->setAuthConfig($oauthCredentialsFile);
$this->client->setRedirectUri($redirectUri);

$this->client->addScope('https://www.googleapis.com/auth/userinfo.profile'); 
$this->client->addScope('https://www.googleapis.com/auth/userinfo.email'); 

I've checked the values of the OAuth values and $me = $oauth2Service->userinfo_v2_me->get();, but nothing helps.

Is there anything I can add to determine which user (/u/1) I am currently?

I know that for most users of the intranet they will only be logged into one Google Account and I can therefore just use /u/0, but this seems clunky.

I've also found that I can download the thumbnail photo in Google Apps Script, but this size is limited to 96x96 pixels and I could not find a way of getting a larger size.


Solution

  • OK, I had another look at this.

    This method attempts to load an image only accessible by the relevant user.

    Supply this image as the baseUrl and remove any authuser elements. The code then tries to load the image using ?authuser=0 ... ?authuser=5.

    It doesn't seem to work in incognito mode though.

    function determineValidAuthuser()
    {
        let baseUrl = "https://lh3.google.com/d/1oA508StlM2QV8EoW9Zhz9SPhRXoxscZp=s320-w320-h200-k-p";
    
        return new Promise((resolve, reject) => 
        {
            var attemptLoad = function(i)
            {
                let img = new Image();
                img.onload = function()
                {
                    resolve(authuser);
                }
    
                img.onerror = function()
                {
                    if(authuser++ > 5)
                        reject();
                    else
                        attemptLoad(authuser);
                }
    
                img.src = baseUrl + `?authuser=${i}`;
            }
    
            var authuser = 0;
            attemptLoad(authuser);
        });
    }
    
    determineValidAuthuser().then(function(authuser)
    {
        console.log(`The authuser is ${authuser}`);
    }).catch(function()
    {
        console.log(`The authuser is unknown, perhaps try 0 and see what happens...`);
    });