通过 UID 获取 Google 个人资料图片 url

Fetch Google profile image url by UID

我想在获得 UID 时获取 google 个人资料图片。 我有 this 参考,但我正在了解如何打这个 url 。 请任何人给我一些例子。

编辑: 这是我的代码

var googleImageUrl: URL?{
    let urlString = "https://www.googleapis.com/plus/v1/people/\(uid)?fields=image&key=AIzaSyBfjHpl8DjU0IGw9mXbvK6HoNpY"
    return URL(string: urlString)
}

Alamofire.request(url)
    .validate()
    .responseJSON(completionHandler: { (response) in
        if response.result.isSuccess {
            let json = response.result.value as? [String: Any]


        } else {
            let apiError = ApiError(response: response)

        }
    })

点击此 api 时,我总是收到错误消息。为什么我没有收到回复?

您有两个选择:

使用 Google API,需要 API 密钥:

https://www.googleapis.com/plus/v1/people/{UID}?fields=image&key={YOUR_API_KEY}

使用不需要任何 API 密钥的 Googles Picasaweb:

http://picasaweb.google.com/data/entry/api/user/{UID}?alt=json

获取图片的代码:

// Check what you want to return in the onCompletion and use it
func getImage(uid: String, onCompletion: @escaping (String) -> Void, onError: @escaping (NSError) -> Void) {
    let url = "https://www.googleapis.com/plus/v1/people/\(uid)?fields=image&key=AIzaSyBfjHpl8DjU0IGw9mXbvK6HoNpY"

    let headers = [
        "Content-Type": "application/json"
    ]

    Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in
            if let object = response.result.value as? [String:Any] {
                print(object)
                // use the onCompletion in here
            }
    }
}

您列出的引用指向 Google 人 API,这与 Google+ 人 API 不同。如果你想使用 Google 人 API 你应该使用 https://people.googleapis.com/v1/people/{UID}?personFields=photo&key={YOUR_API_KEY}

有官方示例:https://developers.google.com/people/v1/read-people#get-the-person-for-a-google-account