【问题标题】:Is there any sample/example available to use Google People API for iOS?是否有任何示例/示例可用于使用适用于 iOS 的 Google People API?
【发布时间】:2018-05-02 07:29:19
【问题描述】:

我浏览了 Google People API 的文档。

https://developers.google.com/people/v1/getting-started

我找不到任何帮助/示例来为 iOS 平台实现相同的功能。 iOS平台有帮助吗?

【问题讨论】:

  • @VidyaMurthy 他要的是人而不是地方。
  • Google 似乎没有适用于 iOS 的 People API 客户端库。所以你必须自己实现模型并按照他们的 REST API 指南来实现网络调用。
  • 谢谢@AuRis。同意。 Google 没有适用于 iOS 的 People API 客户端库。在其他任何地方也找不到相同的。似乎需要我们自己实现。
  • @Niraj 你得到任何登录用户详细信息,如电话号码等?

标签: ios swift google-people-api


【解决方案1】:

是的,有一个适用于 iOS 的 People API 客户端库。要包含它,您需要指定

pod 'GoogleAPIClientForREST/PeopleService', '~> 1.3.4'
pod 'GoogleSignIn', '~> 4.1.2'

在 Cocoapods pod 文件中。

不,除了https://github.com/google/google-api-objectivec-client-for-rest 的源库本身之外,我没有找到任何适用于 iOS 的 Google People API 客户端库的文档

使用 Google Calendar API iOS Swift 示例代码作为指导,设置

private let scopes = [kGTLRAuthScopePeopleServiceContactsReadonly]
private let service = GTLRPeopleServiceService()

以下代码读取已登录用户的联系人列表。

// MARK: - Get Google Contacts

@objc func fetchContacts() {
    let query = GTLRPeopleServiceQuery_PeopleConnectionsList.query(withResourceName: "people/me")
    query.personFields = "names,emailAddresses,photos"
    service2.executeQuery(
        query,
        delegate: self,
        didFinish: #selector(getCreatorFromTicket(ticket:finishedWithObject:error:)))
}

@objc func getCreatorFromTicket(
    ticket: GTLRServiceTicket,
    finishedWithObject response: GTLRPeopleService_ListConnectionsResponse,
    error: NSError?) {

    if let error = error {
        showAlert(title: "Error", message: error.localizedDescription)
        return
    }

    if let connections = response.connections, !connections.isEmpty {
        for connection in connections {
            if let names = connection.names, !names.isEmpty {
                for name in names {
                    if let _ = name.metadata?.primary {
                        print(name.displayName ?? "")
                    }
                }
            }
            if let emailAddresses = connection.emailAddresses, !emailAddresses.isEmpty {
                for email in emailAddresses {
                        if let _ = email.metadata?.primary {
                    print(email.value ?? "")
                    }
                }
            }
            if let photos = connection.photos, !photos.isEmpty {
                for photo in photos {
                    if let _ = photo.metadata?.primary {
                        print(photo.url ?? "")
                    }
                }
            }
        }
    }
}

ps 要使 Google Calendar API iOS Swift 示例构建没有错误,您可能需要添加一个桥接头文件(文件 > 新建 > 文件,选择头文件)并添加以下内容:

#import <GTMSessionFetcher/GTMSessionFetcher.h>
#import <GTMSessionFetcher/GTMSessionFetcherService.h>

然后在 Swift Compiler - General 标题下的目标 Build Settings 中,在 ObjectiveC Bridging 标题行中添加

projectname/bridgingheaderfilename

您可能还需要清理构建文件夹(产品菜单,按住选项键)。

【讨论】:

【解决方案2】:

在 Swift 4.2 中添加 Hugo 的答案,如果您使用 GoogleSignIn cocoapod,您需要使用 GIDSignInDelegate 进行身份验证,一旦您符合此协议,您可以将以下内容添加到您的类 init() 以及可选的字符串

private var accessToken: String?

//init
GIDSignIn.sharedInstance().clientID = "Your client id via https://console.developers.google.com/apis/api/people.googleapis.com"
GIDSignIn.sharedInstance().delegate = self

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let error = error {
        print("Google sign in error: \(String(describing: error.localizedDescription))")
        return
    }

    guard let authentication = user.authentication else { return }
    self.accessToken = authentication.accessToken
    self.fetchContacts()
}

现在fetchContacts() 看起来像这样:

func fetchContacts() {

    let query = GTLRPeopleServiceQuery_PeopleConnectionsList.query(withResourceName: "people/me")
    let formattedToken = String(format: "Bearer %@", self.accessToken!)
    let headers = ["Authorization": formattedToken, "3.0": "GData-Version"]
    query.additionalHTTPHeaders = headers
    query.personFields = "names,emailAddresses,photos"
    query.pageSize = 2000 //max 
    services.shouldFetchNextPages = true
    services.executeQuery(
        query,
        delegate: self,
        didFinish: #selector(getCreatorFromTicket(ticket:finishedWithObject:error:)))
}

在 AppDelegate.swift 中,您还需要调用此方法,以便在身份验证结束时正确处理 url

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    return GIDSignIn.sharedInstance().handle(url,
                                             sourceApplication:options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                                             annotation: [:])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多