我知道很多社交网络API提供了一种使用user_id或用户名构建用户个人资料图片网址的方法。对于Facebook,它看起来像这样:
http://graph.facebook.com/user_id/picture?type=square
现在Google Plus有这样的东西吗?或者任何其他方式在没有API调用的情况下获取用户的图片?
谷歌已经改变了他们的政策,因此获取谷歌个人资料图片的旧方法现在不起作用了
https://plus.google.com/s2/photos/profile/(user_id)?sz=150
这样做的新方法是
请求网址
https://www.googleapis.com/plus/v1/people/115950284...320?fields=image&key={YOUR_API_KEY}
这将以json格式提供Google个人资料图片网址,如下所示
回应:
{
"image":
{
"url": "https://lh3.googleusercontent.com/-OkM...AANA/ltpH4BFZ2as/photo.jpg?sz=50"
}
}
可以找到更多参数来发送您可能需要从here获取的URL
有关更多详细信息,您还可以查看给出的问题我在哪里回答了相同类型的问题How to get user image through user id in Google plus?
快乐编码!!
尝试了一切可能..这里是最后一段工作代码。希望它可以帮助正在寻找它的人。
<?
$url='https://www.googleapis.com/plus/v1/people/116599978027440206136?fields=image%2Furl&key=MY_API_KEY&fields=image';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$d = json_decode($response);
$avatar_url = $d->{'image'}->{'url'};
echo $avatar_url;
?>
如果你使用Flutter,那么你可以通过people.googleapis.com
端点访问它,代码使用google_sign_in
库
import 'package:google_sign_in/google_sign_in.dart';
Future<String> getPhotoUrl(GoogleSignInAccount account, String userId) async {
// final authentication = await account.authentication;
final url = 'https://people.googleapis.com/v1/people/${userId}?personFields=photos';
final response = await http.get(
url,
headers: await account.authHeaders
);
final data = json.decode(response.body);
return data['photos'].first['url'];
}
你会得到类似的东西
{
resourceName: people/998812322529259873423,
etag: %EgQBAzcabcQBAgUH,
photos: [{metadata: {primary: true, source: {type: PROFILE, id: 107721622529987673423}},
url: https://lh3.googleusercontent.com/a-/abcdefmB2p1VWxLsNT9WSV0yqwuwo6o2Ba21sh_ra7CnrZ=s100}]
}
其中url
是一个可访问的图像网址。
简单回答:不
您将不得不查询人员API并获取配置文件image.url数据以获取照片。 AFAIK没有包含userID的url的默认格式。
更新:以下方法自2015年起不起作用
可以获取个人资料图片,您甚至可以设置它的大小:
https://plus.google.com/s2/photos/profile/<user_id>?sz=<your_desired_size>
示例:我的个人资料照片,大小设置为100像素:
https://plus.google.com/s2/photos/profile/116018066779980863044?sz=100
使用图像标记:
<img src="https://plus.google.com/s2/photos/profile/116018066779980863044?sz=100" width="100" height="100">
希望你能运作!
更新:Google停止了对此方法的支持,现在返回404(未找到)错误。
所有这些网址都会获取用户的个人资料图片:
https://www.google.com/s2/photos/profile/{user_id}
https://plus.google.com/s2/photos/profile/{user_id}
https://profiles.google.com/s2/photos/profile/{user_id}
它们会重定向到您从Google API获得的相同图片网址,这是一个丑陋的链接 lh6.googleusercontent.com/-x1W2-XNKA-A/AAAAAAAAAAI/AAAAAAAAAAA/ooSNulbLz8U/photo.jpg
最简单的是直接使用像图像源:
<img src="https://www.google.com/s2/photos/profile/{user_id}">
否则,要获得与Google API调用完全相同的网址,您可以阅读图片标题, 例如在PHP中:
$headers = get_headers("https://www.google.com/s2/photos/profile/{user_id}", 1);
echo "<img src=$headers[Location]>";
方法1 :(不再有效)
https://plus.google.com/s2/photos/profile/<user_id>?sz=<your_desired_size>
方法2 :(每个请求计入您的api速率限制,即每天10k请求免费)
https://www.googleapis.com/plus/v1/people/<user_id>?fields=image&key={YOUR_API_KEY}
使用以下响应格式:
{ "image": { "url": "lh5.googleusercontent.com/-keLR5zGxWOg/.../photo.jpg?sz=50"; } }
方法3 :(不要求api密钥)
http://picasaweb.google.com/data/entry/api/user/<user_id>?alt=json
在json响应中,您将获得一个名为“gphoto $ thumbnail”的属性,其中包含如下所示的个人资料图片网址:
http://lh6.ggpht.com/-btLsReiDeF0/AAAAAAAAAAI/AAAAAAAAAAA/GXBpycNk984/s64-c/filename.jpg
您可能会在网址中注意到部分“s64-c”,这意味着图像大小为64,我尝试使用其他值,如“s100-c”,他们工作。此外,如果你删除“s64-c”部分并附加“?sz = 100”参数,那么现在也可以使用。虽然这不是获取gplus用户的个人资料图片的好方法,但其优点是不需要任何api密钥。
Google,无需API:
$data = file_get_contents('http://picasaweb.google.com/data/entry/api/user/<USER_ID>?alt=json');
$d = json_decode($data);
$avatar = $d->{'entry'}->{'gphoto$thumbnail'}->{'$t'};
//输出示例:https://lh3.googleusercontent.com/-2N6fRg5OFbM/AAAAAAAAAAI/AAAAAAAAADE/2-RmpExH6iU/s64-c/photo.jpg
更改:大小为“s64”的64
如果要显示当前登录用户的个人资料图片,您甚至不需要知道{user_id}。简单地使用https://plus.google.com/s2/photos/profile/me
就足够了。
您可以使用Google+ API的people.get方法获取个人资料图片的网址。这确实需要额外的往返,但是获得图像的最可靠方式。
您在技术上也可以使用URL https://s2.googleusercontent.com/s2/photos/profile/{id}?sz={size}
,然后重定向到最终的URL。 {id}
是Google用户ID或旧的Google个人资料用户名之一(它们仍然适用于拥有它们的用户,但我认为您不能再创建新的用户名)。 {size}
是所需的照片大小(以像素为单位)。我几乎可以肯定这不是一个记录在案的,支持的功能,所以我不会依赖它来做任何重要的事情,因为它可能会随时消失,恕不另行通知。但对于快速原型或小型一次性应用,它可能就足够了。
尝试访问/ s2 /个人资料/照片网址适用于大多数用户,但不是全部。
唯一的完整校对方法是使用Google+ API。您不需要用户身份验证来请求公共配置文件数据,因此这是一个相当简单的方法:
注意用户名之前的+。如果您使用用户ID(长串数字),则不需要+
旧版Google+ API已于2019年3月7日关闭。最近已对关机计划进行了更改,这可能会减轻其对某些开发人员的影响。