{"id":13367205,"url":"https://github.com/hbagdi/Go-unsplash","last_synced_at":"2025-03-12T18:32:09.841Z","repository":{"id":16309127,"uuid":"79425566","full_name":"hbagdi/go-unsplash","owner":"hbagdi","description":"Go Client for the Unsplash API ","archived":false,"fork":false,"pushed_at":"2023-04-14T21:40:43.000Z","size":166,"stargazers_count":74,"open_issues_count":8,"forks_count":13,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-25T05:25:19.758Z","etag":null,"topics":["api-client","curated-photos","free-photos","go","go-packages","golang","golang-bindings","golang-library","photography","photos","search-photos","unsplash","unsplash-api"],"latest_commit_sha":null,"homepage":"https://unsplash.com","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hbagdi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-19T07:04:04.000Z","updated_at":"2024-09-14T00:54:00.000Z","dependencies_parsed_at":"2024-10-24T14:18:55.814Z","dependency_job_id":"4fe999be-6fcf-4a09-aa65-b78587376f85","html_url":"https://github.com/hbagdi/go-unsplash","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbagdi%2Fgo-unsplash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbagdi%2Fgo-unsplash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbagdi%2Fgo-unsplash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbagdi%2Fgo-unsplash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hbagdi","download_url":"https://codeload.github.com/hbagdi/go-unsplash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243271535,"owners_count":20264472,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-client","curated-photos","free-photos","go","go-packages","golang","golang-bindings","golang-library","photography","photos","search-photos","unsplash","unsplash-api"],"created_at":"2024-07-30T00:01:41.400Z","updated_at":"2025-03-12T18:32:09.563Z","avatar_url":"https://github.com/hbagdi.png","language":"Go","funding_links":[],"categories":["第三方 APIs"],"sub_categories":["高级控制台界面","高級控制台界面"],"readme":"# Unsplash API client\n\n[![GoDoc](https://godoc.org/github.com/hardikbagdi/go-unsplash?status.svg)](https://godoc.org/github.com/hbagdi/go-unsplash/unsplash)\n[![CI Test](https://github.com/hbagdi/go-unsplash/actions/workflows/test.yaml/badge.svg)](https://github.com/hbagdi/go-unsplash/actions/workflows/test.yaml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hbagdi/go-unsplash)](https://goreportcard.com/report/github.com/hbagdi/go-unsplash)\n\nA wrapper for the [Unsplash API](https://unsplash.com/developers).\n\n[Unsplash](https://unsplash.com) provides [freely licensed](https://unsplash.com/license) high-resolution photos that can be used for anything.\n\n## Documentation\n\n- [Installation](#installation)\n- [Dependencies](#dependencies)\n- [API terms and guidelines](#api-guidelines)\n- [Registering your App](#registration)\n- [Contact for help](#help)\n- [Usage](#usage)\n- [License](#license)\n\n## Installation\n\n```go\ngo get github.com/hbagdi/go-unsplash/unsplash\n```\n\n## Dependencies\n\nThis library has a single dependency on Google's [go-querystring](https://github.com/google/go-querystring).\n\n## API Guidelines\n\nWhen using the Unsplash API, you need to make sure to abide by their [API guidelines](https://medium.com/unsplash/unsplash-api-guidelines-28e0216e6daa) and [API Terms](https://unsplash.com/api-terms).\n\n## Registration\n\n[Sign up](https://unsplash.com/join) on Unsplash.com and register as a [developer](https://unsplash.com/developers).\u003cbr\u003e\nYou can then [create a new application](https://unsplash.com/oauth/applications/new) and use the AppID and Secret for authentication.\n\n## Help\n\nPlease open an issue in this repository if you need help or want to report a bug.\u003cbr\u003e\nMail at the e-mail address in the license if needed.\n\n## Usage\n\n- [Importing](#importing)\n- [Authentication](#authentication)\n- [Creating an instance](#creating-an-instance)\n- [Error handling](#error-handling)\n- [Response struct](#response-struct)\n- [Pagination](#pagination) - paging through results\n- [unsplash.Photos](#photos)\n\n  - [Random](#random) - get random photo(s)\n  - [All](#all-photos) - get all photos on unplash.com\n  - [Curated](#curated-photos) - returns a list of curated photos\n  - [Photo](#photo) -get details of a single photo\n  - [Like](#like) - like a photo on the authenticated users' behalf\n  - [Unlike](#unlike) - unlike a photo on the authenticated users' behalf\n  - [Download link](#download-link) - get download link of a photo\n  - [Stats](#stats) - statistics of a photo\n\n- [unsplash.Collections](#collections)\n\n  - [All](#all-collections) - return all collections on unsplash.com\n  - [Collection](#collection) - get details about a single existing collection\n  - [Curated](#curated-collections) - get a list of curated collections\n  - [Featured](#featured-collections) - get a list of featured collections\n  - [Related](#related-collections) - get a list of related collections of a particular collection\n  - [Create](#create-collection) - create a collection for the authenticated user\n  - [Delete](#delete-collection) - delete a collection\n  - [Update](#update-collection) - update a collection's description\n  - [Add Photo](#add-photo) - add a photo to a collection\n  - [Remove Photo](#remove-photo) - remove a photo from a collection\n\n- [unsplash.Users](#users)\n\n  - [User](#user) - get details about a single user\n  - [Portfolio](#portfolio) - get link to a user's portfolio\n  - [Liked Photos](#liked-photos) - get list of photos a user has liked\n  - [Photos](#user-photos) - get list of photos a user has uploaded to unsplash.com\n  - [Collections](#user-collections) - collections of a user\n\n- [unsplash.Search](#search)\n\n  - [Photos](#search-photos) - search photos\n  - [Collections](#search-collections) - search collections\n  - [Users](#search-users) - search users\n\n### Importing\n\nOnce you've installed the library using [`go get`](#installation), import it to your go project:\n\n```go\nimport \"github.com/hbagdi/go-unsplash/unsplash\"\n```\n\n### Authentication\n\nAuthentication is not handled by directly by go-unsplash.\u003cbr\u003e\nInstead, pass an [`http.Client`](https://godoc.org/net/http#Client) that can handle authentication for you.\u003cbr\u003e\nYou can use libraries such as [oauth2](https://godoc.org/golang.org/x/oauth2).\u003cbr\u003e\nPlease note that all calls will include the OAuth token and hence, [`http.Client`](https://godoc.org/net/http#Client) should not be shared between users.\n\nNote that if you're just using actions that require the public permission scope, only the AppID is required.\n\n### Creating an instance\n\nAn instance of unsplash can be created using `New()`.\u003cbr\u003e\nThe http.Client supplied will be used to make requests to the API.\n\n```go\nts := oauth2.StaticTokenSource(\n  // note Client-ID in front of the access token\n  \u0026oauth2.Token{AccessToken: \"Client-ID Your-access-token\"},\n)\nclient := oauth2.NewClient(oauth2.NoContext, ts)\n//use the http.Client to instantiate unsplash\nunsplash := unsplash.New(client)  \n// requests can be now made to the API\nrandomPhoto, _ , err := unsplash.RandomPhoto(nil)\n```\n\n### Error handling\n\nAll API calls return an `error` as second or third return object. All successful calls will return nil in place of this return. Further, go-unsplash has errors defined as types for better error handling.\n\n```go\nrandomPhoto, _ , err := unsplash.RandomPhoto(nil)\nif err != nil {\n  //handle error\n}\n```\n\n### Response struct\n\nMost API methods return a [`*Response`](https://godoc.org/github.com/hbagdi/go-unsplash/unsplash#Response) along-with the result of the call.\u003cbr\u003e\nThis struct contains paging and rate-limit information.\n\n### Pagination\n\nPagination is currently supported by supplying a page number in the [`ListOpt`](https://godoc.org/github.com/hbagdi/go-unsplash/unsplash#ListOpt). The `NextPage` field in Response can be used to get the next page number.\n\n```go\nsearchOpt := \u0026SearchOpt{Query : \"Batman\"}\nphotos, resp, err := unsplash.Search.Photos(searchOpt)\n\nif err != nil {\n  return\n}\n// process photos\nfor _,photo := range *photos {\n  fmt.Println(*photo.ID)\n}\n// get next\nif !resp.HasNextPage {\n  return\n}\nsearchOpt.Page = resp.NextPage\nphotos, resp ,err = unsplash.Search.Photos(searchOpt)\n//photos now has next page of the search result\n```\n\n### Photos\n\nUnsplash.Photos is of type PhotosService.\u003cbr\u003e\nIt provides various methods for querying the /photos endpoint of the API.\n\n#### Random\n\nYou can get a single random photo or multiple depending upon opt. If opt is nil, then a single random photo is returned. Random photos satisfy all the parameters specified in `*RandomPhotoOpt`.\n\n```go\nphotos, resp, err := unsplash.Photos.Random(nil)\nassert.Nil(err)\nassert.NotNil(photos)\nassert.NotNil(resp)\nassert.Equal(1, len(*photos))\nvar opt RandomPhotoOpt\nopt.Count = 3\nphotos, resp, err = unsplash.Photos.Random(\u0026opt)\nassert.Nil(err)\nassert.NotNil(photos)\nassert.NotNil(resp)\nassert.Equal(3, len(*photos))\n```\n\n#### All photos\n\nGet all photos on unsplash.com.\u003cbr\u003e\nObviously, this is a huge list and hence can be paginated through.\n\n```go\nopt := new(unsplash.ListOpt)\nopt.Page = 1\nopt.PerPage = 10\n\nif !opt.Valid() {\n    fmt.Println(\"error with opt\")\n    return\n}\ncount := 0\nfor {\n    photos, resp, err := un.Photos.All(opt)\n\n    if err != nil {\n        fmt.Println(\"error\")\n        return\n    }\n    //process photos\n    for _, c := range *photos {\n        fmt.Printf(\"%d : %d\\n\", count, *c.ID)\n        count += 1\n    }\n    //go for next page\n    if !resp.HasNextPage {\n        return\n    }\n    opt.Page = resp.NextPage\n}\n```\n\n#### Curated Photos\n\nGet all curated photos on unsplash.com.\u003cbr\u003e\nObviously, this is a huge list and hence can be paginated through.\n\n```go\nopt := new(unsplash.ListOpt)\nopt.Page = 1\nopt.PerPage = 10\n\nif !opt.Valid() {\n    fmt.Println(\"error with opt\")\n    return\n}\ncount := 0\nfor {\n    photos, resp, err := un.Photos.Curated(opt)\n\n    if err != nil {\n        fmt.Println(\"error\")\n        return\n    }\n    //process photos\n    for _, c := range *photos {\n        fmt.Printf(\"%d : %d\\n\", count, *c.ID)\n        count += 1\n    }\n    //go for next page\n    if !resp.HasNextPage {\n        return\n    }\n    opt.Page = resp.NextPage\n}\n```\n\n#### Photo\n\nGet details of a specific photo.\n\n```go\nphoto, resp, err := unsplash.Photos.Photo(\"9BoqXzEeQqM\", nil)\nassert.NotNil(photo)\nassert.NotNil(resp)\nassert.Nil(err)\nfmt.Println(photo)\n//photo is of type *Unsplash.Photo\n\n// you can also specify a PhotoOpt to get a custom size or cropped photo\nvar opt PhotoOpt\nopt.Height = 400\nopt.Width = 600\nphoto, resp, err = unsplash.Photos.Photo(\"9BoqXzEeQqM\", \u0026opt)\nassert.NotNil(photo)\nassert.NotNil(resp)\nassert.Nil(err)\nlog.Println(photo)\n//photo.Urls.Custom will have the cropped photo\n// See PhotoOpt for more details\n```\n\n#### Like\n\n```go\n//Like a random photo\nphotos, resp, err := unsplash.Photos.Random(nil)\nassert.Nil(err)\nassert.NotNil(photos)\nassert.NotNil(resp)\nassert.Equal(1, len(*photos))\nphotoid := (*photos)[0].ID\nphoto, resp, err := unsplash.Photos.Like(*photoid)\nassert.Nil(err)\nassert.NotNil(photo)\nassert.NotNil(resp)\n```\n\n#### Unlike\n\nSame way as Like except call `Unlike()`.\n\n#### Download Link\n\nGet download URL for a photo.\n\n```go\nurl, resp, err := unsplash.Photos.DownloadLink(\"-HPhkZcJQNk\")\nassert.Nil(err)\nassert.NotNil(url)\nassert.NotNil(resp)\nlog.Println(url)\n```\n\n#### Stats\n\nStatistics for a specific photo\n\n```go\nstats, resp, err := unsplash.Photos.Stats(\"-HPhkZcJQNk\")\nassert.Nil(err)\nassert.NotNil(stats)\nassert.NotNil(resp)\nlog.Println(stats)\n```\n\n### Collections\n\nVarious details about collection(s).\n\n#### All collections\n\n```go\ncollections, resp, err = unsplash.Collections.All(nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\nopt := new(ListOpt)\nopt.Page = 2\nopt.PerPage = 10\n//get the second page\ncollections, resp, err = unsplash.Collections.All(opt)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\n```\n\n#### Curated collections\n\n```go\ncollections, resp, err := unsplash.Collections.Curated(nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\n```\n\n#### Featured collections\n\nSame as Curated, but use `Featured()` instead.\n\n#### Related collections\n\nGet collections related to a collection.\n\n```go\ncollections, resp, err := unsplash.Collections.Related(\"296\", nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\n//page through if necessary\n```\n\n#### Collection\n\nDetails about a specific collection\n\n```go\ncollection, resp, err := unsplash.Collections.Collection(\"910\")\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collection)\n```\n\n#### Create collection\n\nCreate a collection on behalf of the authenticated user.\n\n```go\nvar opt CollectionOpt\ntitle := \"Test42\"\nopt.Title = \u0026title\ncollection, resp, err := unsplash.Collections.Create(\u0026opt)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collection)\n```\n\n#### Delete collection\n\nLet's delete the collection just created above.\n\n```go\n//get list of collections of a user\ncollections, resp, err := unsplash.Users.Collections(\"gopher\", nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\n//take the first one\ncollection := (*collections)[0]\nassert.NotNil(collection)\n//delete it\nresp, err = unsplash.Collections.Delete(*collection.ID)\nassert.NotNil(resp)\nassert.Nil(err)\n```\n\n#### Update collection\n\n```go\n//get a user's collection\ncollections, resp, err := unsplash.Users.Collections(\"gopher\", nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\n// take the first one\ncollection := (*collections)[0]\nassert.NotNil(collection)\nlog.Println(*collection.ID)\n//random title\nvar opt CollectionOpt\ntitle := \"Test43\" + strconv.Itoa(rand.Int())\nopt.Title = \u0026title\n//update the title\ncol, resp, err := unsplash.Collections.Update(*collection.ID, \u0026opt)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(col)\n```\n\n#### Add photo\n\n```go\nphotos, resp, err := unsplash.Photos.Random(nil)\nphoto := (*photos)[0]\n//get a user's collection\ncollections, resp, err := unsplash.Users.Collections(\"gopher\", nil)\nassert.Nil(err)\ncollection := (*collections)[0]\n//add the photo\nresp, err = unsplash.Collections.AddPhoto(*collection.ID, *photo.ID)\nassert.Nil(err)\n```\n\n#### Remove photo\n\n```go\n//remove a photo\n_, _ = unsplash.Collections.RemovePhoto(*collection.ID, *photo.ID)\n```\n\n### Users\n\nDetails about an unsplash.com users.\n\n#### User\n\nDetails about unsplash.com users.\n\n```go\nprofileImageOpt := \u0026ProfileImageOpt{Height: 120, Width: 400}\n//or pass a nil as second arg\nuser, err := unsplash.Users.User(\"lukechesser\", profileImageOpt)\nassert.Nil(err)\nassert.NotNil(user)\n\n//OR, get the currently authenticated user\nuser, resp, err := unsplash.CurrentUser()\nassert.Nil(user)\nassert.Nil(resp)\nassert.NotNil(err)\n```\n\n#### Portfolio\n\n```go\nurl, err = unsplash.Users.Portfolio(\"gopher\")\nassert.Nil(err)\nassert.NotNil(url)\nassert.Equal(url.String(), \"https://wikipedia.org/wiki/Gopher\")\n```\n\n#### Liked Photos\n\n```go\nphotos, resp, err := unsplash.Users.LikedPhotos(\"lukechesser\", nil)\nassert.Nil(err)\nassert.NotNil(photos)\nassert.NotNil(resp)\n```\n\n#### User photos\n\nGet photos a users has uploaded on unsplash.com\n\n```go\nphotos, resp, err := unsplash.Users.Photos(\"lukechesser\", nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(photos)\n```\n\n#### User collections\n\nGet a list of collections created by the user.\n\n```go\ncollections, resp, err := unsplash.Users.Collections(\"gopher\", nil)\nassert.Nil(err)\nassert.NotNil(resp)\nassert.NotNil(collections)\n```\n\n### Search\n\nSearch for photos, collections or users.\n\n#### Search photos\n\n```go\nvar opt SearchOpt\n//an empty search will be erroneous\nphotos, resp, err := unsplash.Search.Photos(\u0026opt)\nassert.NotNil(err)\nassert.Nil(resp)\nassert.Nil(photos)\nopt.Query = \"Nature\"\n//Search for photos tageed \"Nature\"\nphotos, _, err = unsplash.Search.Photos(\u0026opt)\nlog.Println(len(*photos.Results))\nassert.NotNil(photos)\nassert.Nil(err)\n```\n\n#### Search collections\n\n```go\nvar opt SearchOpt\nopt.Query = \"Nature\"\ncollections, _, err = unsplash.Search.Collections(\u0026opt)\nassert.NotNil(collections)\nassert.Nil(err)\nlog.Println(len(*collections.Results))\n```\n\n#### Search users\n\n```go\nvar opt SearchOpt\nopt.Query = \"Nature\"\nusers, _, err = unsplash.Search.Users(\u0026opt)\nlog.Println(len(*users.Results))\nassert.NotNil(users)\nassert.Nil(err)\n```\n\n## License\n\nCopyright (c) 2017 Hardik Bagdi [hbagdi1@binghamton.edu](mailto:hbagdi1@binghamton.edu)\n\nMIT License\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhbagdi%2FGo-unsplash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhbagdi%2FGo-unsplash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhbagdi%2FGo-unsplash/lists"}