【问题标题】:Fetch Google profile image url by UID按 UID 获取 Google 个人资料图片网址
【发布时间】:2017-09-12 06:05:06
【问题描述】:

当给我 UID 时,我想获取 google 个人资料图片。 我有this 参考,但我知道如何点击这个网址。 请任何人给我一些例子。

编辑: 这是我的代码

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 时,我总是得到错误。为什么我没有收到回复?

【问题讨论】:

标签: ios swift google-people-api gidsignin


【解决方案1】:

你有两个选择:

使用 Googles 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
            }
    }
}

【讨论】:

  • 我应该使用 alamofire 或谷歌框架中的任何东西点击这个 url 吗?
  • 我会推荐 Alamofire,它是一个很好的 HTTP 网络工具,并且有据可查 @PreetiRani
【解决方案2】:

您列出的参考指向 Google People API,它不同于 Google+ People API。如果你想使用 Google People 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

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多