main.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2017, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import gRPC
  18. import OAuth2
  19. let CREDENTIALS = "google.yaml" // in $HOME/.credentials
  20. let TOKEN = "google.json" // local auth token storage
  21. #if os(OSX)
  22. // On OS X, we use the local browser to help the user get a token.
  23. let tokenProvider = try BrowserTokenProvider(credentials:CREDENTIALS, token:TOKEN)
  24. if tokenProvider.token == nil {
  25. try tokenProvider.signIn(scopes:["profile",
  26. "https://www.googleapis.com/auth/contacts.readonly",
  27. "https://www.googleapis.com/auth/cloud-platform"])
  28. try tokenProvider.saveToken(TOKEN)
  29. }
  30. #else
  31. // On Linux, we can get a token if we are running in Google Cloud Shell
  32. // or in some other Google Cloud instance (GAE, GKE, GCE, etc).
  33. let tokenProvider = try GoogleTokenProvider()
  34. #endif
  35. gRPC.initialize()
  36. guard let authToken = tokenProvider.token?.accessToken else {
  37. print("ERROR: No OAuth token is avaiable.")
  38. exit(-1)
  39. }
  40. let projectID = "YOUR PROJECT ID"
  41. let certificateURL = URL(fileURLWithPath:"roots.pem")
  42. let certificates = try! String(contentsOf: certificateURL, encoding: .utf8)
  43. let service = Google_Datastore_V1_DatastoreService(address:"datastore.googleapis.com",
  44. certificates:certificates,
  45. host:nil)
  46. service.metadata = Metadata(["authorization":"Bearer " + authToken])
  47. var request = Google_Datastore_V1_RunQueryRequest()
  48. request.projectID = projectID
  49. var query = Google_Datastore_V1_GqlQuery()
  50. query.queryString = "select *"
  51. request.gqlQuery = query
  52. print("\(request)")
  53. do {
  54. let result = try service.runquery(request)
  55. print("\(result)")
  56. } catch (let error) {
  57. print("ERROR: \(error)")
  58. }