Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. {
  2. "code" : 401,
  3. "errors" : [ {
  4. "domain" : "global",
  5. "location" : "Authorization",
  6. "locationType" : "header",
  7. "message" : "Invalid Credentials",
  8. "reason" : "authError"
  9. } ],
  10. "message" : "Invalid Credentials"
  11. }
  12.  
  13. val credential = GoogleCredential().setAccessToken(token)
  14.  
  15. val customsearch = Customsearch.Builder(NetHttpTransport(), JacksonFactory(), credential)
  16. .setApplicationName(appName)
  17. .build()
  18.  
  19. val list = customsearch.cse().list("image name")
  20. list.searchType = "image"
  21. val search = list.execute()
  22. val items = search.items
  23. items
  24. .forEach {
  25. println(it.displayLink)
  26. }
  27.  
  28. /**
  29. * Класс для поиска изображений через Google Custom API
  30. *
  31. * 1) Создать проект с именем {@param projectName} на странице https://console.developers.google.com/apis/
  32. * 2) Включить Custom Search API и запомнить API key {@param apiKey}
  33. * 3) Создать пользовательский поиск https://cse.google.com/cse/all, запомнить id поисковой системы {@param cx},
  34. * при редактировании включить поиск изображений по умолчанию он выключен
  35. */
  36. class GoogleImageSearch(val apiKey: String, val cx: String, projectName: String) {
  37. private val builder = Customsearch.Builder(NetHttpTransport(), JacksonFactory(), null)
  38. private val customsearch: Customsearch
  39. private val cse: Customsearch.Cse
  40.  
  41. init {
  42. builder.applicationName = projectName
  43. customsearch = builder.build()
  44. cse = customsearch.cse()
  45. }
  46.  
  47. /**
  48. * @param query - Поисковый запрос
  49. * @param imageSize - размер картинки
  50. * @param index - начальный индекс результатов
  51. * @return результаты запроса
  52. */
  53. fun search(query: String, imageSize: String = "medium", index: Long = 1): MutableList<Result>? {
  54. val list = cse.list(query)
  55. list.key = apiKey
  56. list.cx = cx
  57. list.searchType = "image"
  58. list.imgSize = imageSize
  59. list.start = index
  60. val search = list.execute()
  61. return search.items
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement