如何检查用户的个人资料图片是默认的还是在Google上传?
注意:从API获取用户详细信息时。
答案 0 :(得分:6)
people.get在图片对象中包含isDefault
值。例如。如果你试试+Google
,你会得到;
"image": {
"url": "https://lh4.googleusercontent.com/-v0soe-ievYE/AAAAAAAAAAI/AAAAAAACyas/yR1_yhwBcBA/photo.jpg?sz=50",
"isDefault": false
}
答案 1 :(得分:6)
people.get不再将isDefault作为属性。 https://developers.google.com/+/web/api/rest/latest/people#resource
答案 2 :(得分:0)
in ruby with devise and omniauth-google-oauth2
你的devise.rb中的
config.omniauth :google_oauth2 (Other options....), skip_image_info: false
然后在你的user.rb /其他地方:
def self.parse_auth_image(auth)
if auth.provider == "google_oauth2"
return nil if auth.dig("extra", "raw_info", "image", "isDefault")
end
auth.info.image
end
答案 3 :(得分:0)
在完整细节中执行此操作的最佳方式:
d <- c('01-Apr','01-Mar','02-Jan','31-June','30-May')
d.new <- as.Date(d, format = '%d-%b')
d.new <- d.new[order(d.new)]
d.new
#[1] "2018-01-02" "2018-03-01" "2018-04-01" "2018-05-30" NA
默认图片:
require 'open-uri'
要检查的图片:
default = "https://lh3.googleusercontent.com/-
XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg"
比较检查
image_to_check = "https://lh3.googleusercontent.com/-
uh4wK1JDtCI/AAAAAAAAAAI/AAAAAAAAAAA/huieBSbV4zg/s64-
c/100695019739939096169.jpg"
然后你比较2个图像blob:
blob_for_default_image = open(default).read
blob_for_image_to_check = open(image_to_check).read
答案 4 :(得分:0)
在2020年更新此答案:现在可以通过向people.get
API发送请求,并以photos
作为personFields
来获取用户的个人资料图片。
您将获得一组图像,并且只要出现default: true
,就表示它是默认的(不是用户设置的)图像:
示例(如果您将其与OAuth结合使用)
获取https://people.googleapis.com/v1/people/me
示例响应(带有个人资料图片)
{
"resourceName": "people/117055959617985617271",
"etag": "%EgQBAzcuGgQBAgUHIgxHamp6MG9wZ3hOcz0=",
"photos": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "117055959617985617271"
}
},
"url": "https://lh3.googleusercontent.com/a-/AOh14Gg_-udXd3O6K1URTBTEK2rLa2DOh6G2dgmOHcOBOtE=s100"
},
{
"metadata": {
"source": {
"type": "CONTACT",
"id": "2"
}
},
"url": "https://lh3.googleusercontent.com/a/default-user=s100",
"default": true
}
]
}
示例响应(仅默认值)
{
"resourceName": "people/113009277022343767972",
"etag": "%EgQBAzcuGgQBAgUHIgxxdHVTY3IxZVJIUT0=",
"photos": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "113009277022343767972"
}
},
"url": "https://lh6.googleusercontent.com/-6NS3awoYRXw/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnTo-mElIpcAEazDV9DAs1VyYDEIw/s100/photo.jpg",
"default": true
}
]
}
答案 5 :(得分:0)
如果你用的是PHP,很简单,直接用这段代码
$profile_img_end = explode("/", $userData['picture']); // Exploding the URL and Dividing it into several terms
if (end($profile_img_end) === "photo.jpg") { // Now Simply Check if last part of array is equal to "photo.jpg" which is common in all default images
$profile_img = null; // If it's default then set it equal to null
} else {
$profile_img = $userData['picture']; // Else get the Link of the Image
}
JavaScript 中的替代品
var url = ""; // Image URL
var img_split = url.split("/"); // Split it Again from / (forward slash)
if (img_split[img_split.length - 1] === 'photo.jpg') { // Check if last array is equal to photo.jpg (By Default Image)
var image = null;
} else {
var image = url;
}
希望有帮助!