101

hello everyone I am trying to load google profile picture in my site and other ones

I have done

var profile = googleUser.getBasicProfile();
profile.getImageUrl()

when I sign in with google and save the image url to a database but when I try to put it into the scr of an img tag like so

var img = document.createElement("img");
img.src = image;
img.alt = "image";
img.style.float = "left";
divn.appendChild(img);

I get 403 forbidden error sometimes, but sometimes it works here is a sample link that I'm using the one that is stored in the database just altered a bit

https://lh4.googleusercontent.com/-OmV9386WzGk/AAAAFFFFAAI/AAAAAAAACpc/BEtVNh85tnk/s96-c/photo.jpg

so I'm just wondering if I'm doing it right obtaining the profile image, and its for other users also on the same page

JRowan
  • 6,824
  • 8
  • 40
  • 59

11 Answers11

184

Using referrerpolicy="no-referrer" seems to help. While it didn't work in a localhost app (before I added this attribute), it worked consistently when loading the image in its own tab. One of the differences in the request headers was the absence of referer.

Michael Ganß
  • 2,846
  • 1
  • 18
  • 11
87

Add referrerpolicy="no-referrer" attribute

<img src="your-google-link-here" referrerpolicy="no-referrer"/>

See about referrerpolicy here

willf80
  • 973
  • 7
  • 11
8

The only thing that worked for me was adding in

<meta name="referrer" content="no-referrer" />

to the head of my html, as explained here: https://github.com/chakra-ui/chakra-ui/issues/5909

Philip Sopher
  • 627
  • 2
  • 8
  • 19
6

I was getting this error when accessing the localhost without any protocol mentioned. If your images aren't loading, try reopening the website giving http:// or https:// That should solve the issue

Arju S Moon
  • 61
  • 1
  • 1
  • This is correct. To test this I started a ngrok.io tunnel and the image was loading through the tunnel even though it was not working in localhost. – Anees Hameed May 27 '21 at 15:40
4

Looks like google doesn't serve requests when the Referer header is set to localhost. I changed it in the inspecto to some other domain, resent it and it worked. I could even get the profile picture with curl and zero headers.

zolee
  • 429
  • 4
  • 19
2

2021 same issue

If you are using the CORS plugin on browser(chrome), please turn it off.

After off the plugin, restart your browser and try again.

.. it works for me!

HARU
  • 21
  • 2
1

I could imagine that the URL is dynamically created each time you request it. That is supported by the fact that the user needs to be authenticated to retrieve that URL. (If the user e.g. signs out of a previously authorized service / revokes the authentication, a service should no longer be able to retrieve the profile picture)

So either you store the entire image as a blob in the database or authenticate and use the User Object each time to request the URL.

Also, consider using the API as referenced here.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Patrick
  • 303
  • 1
  • 4
  • 11
1

I was facing same error in my Next.js app while rendering the user profile picture on MUI <Avatar /> component.

It got fixed and picture was rendered correctly after adding this line on _app.js

<meta name="referrer" content="no-referrer" />
Bibek Oli
  • 366
  • 2
  • 14
0

The link sent back is NOT dynamic. For example my profile picture is this one:

https://lh3.googleusercontent.com/a-/AAuE7mB9nRJ8QQy4bJ97P2zeqX_5u7bVuS_DzxOsV0u6rlc

And I can see my photo even in an incognito tab and while I am logged off.

MehranTM
  • 714
  • 9
  • 10
0

I was able to resolve this in one of my projects, the issue was that I had been using the wrong img tag, i.e. <img src"foo.png" /> instead of <img src="foo.png></img>. The latter is appropriate for DOCTYPE=HTML, whereas the former is meant for XHTML pages.

Phoeobe
  • 1
  • 1
-2

Yup...there's a small hack to fix this. Just change your tag to a closing one, . Apparently, this fixes it, but I'm not sure if this is a temporary solution.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 11:34
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 18:32