Giter Site home page Giter Site logo

gphotos's Introduction

GPhotos

CI Status Version License Platform

I wanted to consume the Google Photos API in Swift but at the time of writing there is no framework that does it in a simple way.

So why not share my own take?

List of implemented methods:

  • Authentication

    • Auto-refresh token
    • Auto-request authorization
  • Albums

    • addEnrichment - Adds an enrichment at a specified position in a defined album.
    • batchAddMediaItems - Adds one or more media items in a user's Google Photos library to an album.
    • batchRemoveMediaItems - Removes one or more media items from a specified album.
    • create - Creates an album in a user's Google Photos library.
    • get - Returns the album based on the specified albumId.
    • list - Lists all albums shown to a user in the Albums tab of the Google Photos app.
    • share - Marks an album as shared and accessible to other users.
    • unshare - Marks a previously shared album as private.
  • Shared albums

    • get - Returns the album based on the specified shareToken.
    • join - Joins a shared album on behalf of the Google Photos user.
    • leave - Leaves a previously-joined shared album on behalf of the Google Photos user.
    • list - Lists all shared albums available in the Sharing tab of the user's Google Photos app.
  • MediaItems

    • batchCreate - Creates one or more media items in a user's Google Photos library.
    • batchGet - Returns the list of media items for the specified media item identifiers.
    • get - Returns the media item for the specified media item identifier.
    • list - List all media items from a user's Google Photos library.
    • search - Searches for media items in a user's Google Photos library.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

To install through CocoaPods add pod 'GPhotos' to your Podfile and run pod install.

Setup

  • Setup a OAuth 2.0 client ID as described here, download the .plist file and add it to the project.

  • In AppDelegate.swift configure GPhotos when the application finishes launching

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    var config = Config()
    config.printLogs = false
    GPhotos.initialize(with: config)
    // Other configs
}
  • To handle redirects during the authorization process add or edit the following method.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let gphotosHandled = GPhotos.continueAuthorizationFlow(with: url)
    // other app links
    return gphotosHandled
}

Usage

Authentication

  • GPhotos.isAuthorized will return true if a user is already authorized.

  • GPhotos.logout() clears the session information and invalidates any token saved.

  • GPhotos.authorize(with scopes:) by default starts the authentication process with openid scope. Will be executed only if there are new scopes. As per Google recommendation, you should gradually add scopes when you need to use them, not on the first run. The method will return a boolean indicating the success status, and an error if any.

  • GPhotos.switchAccount(with scopes:) by default starts the authentication process with openid scope. Will ignore current authentication scopes. The method will return a boolean indicating the success status, and an error if any.

Photos Api

Save an instance of GPhotosApi to be able to use pagination between different features.

Albums

create

  • create(album:) returns the newly created album object

list

  • list() loads sequential pages of items every time it is called.
  • reloadList() loads always the first page.

get

  • get(id:) returns the Album for the provided id.

sharing

  • share(id:options:) shares an album with the provided id with possible options and returns the ShareInfo of the album.
  • unshare(id:) returns a boolean indicating the success status.

enrichments

  • addEnrichment(id:enrichment:position)

media items

Adding or removing items from an album only requires the set of media items ids.

  • addMediaItems(id:mediaIds:)
  • removeMediaItems(id:mediaIds:)

MediaItems

list

  • list() and reloadList() have the same use as in Albums

get

  • get(id:) returns the MediaItem for the provided id.
  • getBatch(ids:) returns the MediaItems for the provided array of ids.

search

  • search(with request:) loads sequential pages of items every time it is called. Results are based on filters in the request. If no filters are applied it will return the same results as list()
  • reloadSearch(with request:) loads always the first page.

batchCreate

  • upload(images:) takes an array of UIImage and uploads them one by one to the user's library. These uploads will count towards storage in the user's Google Account.

SharedAlbums

get

  • get(token:) returns the details of the shared album.

joining

  • join(token:) and leave(token:) take the sharing token as argument and either join or leave the shared album if the token is correct.

list

  • list() and reloadList() have the same use as in Albums

License

GPhotos is available under the MIT license. See the LICENSE file for more info.

gphotos's People

Contributors

axelguilmin avatar deivitaka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gphotos's Issues

Albums.share Not Working?

GPhotos is great!!
But Album.share is not working for my environment.
(XCode 11.3.1, Swift5)

Error Message Below.
GPhotos E: Optional("Invalid JSON payload received. Unexpected token.\nsharedAlbumOptions%5\n^")

Please tell me how to fix this.

Upload crashes if filenames array is not set

If I don't set a filenames array in the upload function, the function crashes at the following line:

self.upload(image: images.popLast()!, filename: filenames.popLast()!) { (token) in

As a workaround I create an array with empty filenames:

let filenames = [String?](repeating: nil, count: images.count)

Image Uploading Issue.

Hello Team,

I'm using GPhotos to upload the images but i'm facing some issue. I have try to uploading the image second time, got correct response from api but the image not shown in the Google Photos. Could you please check and update me, how to solve this issue.

Thank you.

Retaining EXIF data on upload

I'm running into some issues where I upload a portrait photo from iOS that gets rendered as a landscape photo in Google Photos. From my testing it seems to most likely be the result of loading it as a UIImage and calling pngData(). Would it be possible to have MediaItems.Item support Data instead, to allow lossless image uploading to Google Photos, uploading videos, etc?

Base Url and Product Url showing error

Always base Url and product Url showing error when we are trying with this library

GPHOTOS
Screenshot 2022-04-22 at 11 06 16 PM

POSTMAN RESULT
When doing same request with postman giving correct output

Screenshot 2022-04-23 at 12 28 36 AM

Only a few shared albums are listed

I started to use this library and tried to retrieve a list of all my shared albums. But I only get 18 albums, I have much more. Could it be, that there is some problem with pagination with shared albums?

Error handling

I see that it looks like API errors are getting logged out properly via a handle() method, but would it make sense to expose these in the method completion as well? Right now, for instance, if a MediaItem upload fails I just receive res as [], or if adding photos to an album fails I just receive false. In my case this is because I'm uploading a lot of photos and getting 429 Quota exceeded API responses, and I'd like to back off and try again. Whereas with other errors, I'd probably want to just give up and show an error message.

I assume it's a bit more complicated by the fact that the failure could be an HTTP status error or it could be something lower down the stack, so I assume we'd either need two extra arguments to the completion (maybe Status? and Error?) or a single Error? that could be switched to either a Status or other type of Error. But I'm not as familiar with Swift error handling best practices so feel free to disregard these suggestions.

List mediaItems without the readDevData authorization scope

I love this library ๐Ÿ‘
I use it to list users pictures, in a purely read-only fashion, my code looks like this :

GPhotos.initialize(with: Config())
GPhotos.authorize(with: [.openId, .readOnly], completion: { _, _ in
    GPhotosApi.mediaItems.list(completion: { _ in })
})

This asks the user for 2 authorizations (.readOnly and .readDevData) :
Screenshot 2020-07-05 at 16 17 53
But I don't need the .readDevData authorization (at least I think so)

In GPhotosResource.swift :

internal struct scopes {
    static let read: ScopeSet = ([.readOnly, .readDevData], [.readAndAppend])

Both are required, so changing the line 16 to this would solve my issue :

static let read: ScopeSet = ([.readOnly], [.readDevData, .readAndAppend])

However I'm not sure this is the way to go, is it the wanted behavior to always ask for .readDevData alongside .readOnly ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.