Documentation ¶
Overview ¶
Package gphotos provides a client for using the Google Photos API. Wraps the Google Photos package provided by Google, and now removed: https://github.com/denysvitali/go-googlephotos.
Usage:
import "github.com/gphotosuploader/google-photos-api-client-go/lib-gphotos"
Construct a new Google Photos client, then use the various services on the client to access different parts of the Google Photos API. For example:
// httpClient is an authenticated http.Client. See Authentication below. client := gphotos.NewClient(httpClient) // get or create a Photos Album with the specified name. album, err := GetOrCreateAlbumByName("my-new-album") // upload an specified file to an existent Photos Album. _, err := client.AddMediaItem(ctx, path, albumID)
NOTE: Using the https://godoc.org/context package, one can easily pass cancellation signals and deadlines to various services of the client for handling a request. In case there is no context available, then context.Background() can be used as a starting point.
Authentication The gphotos library does not directly handle authentication. Instead, when creating a new client, pass an http.Client that can handle authentication for you. The easiest and recommended way to do this is using the golang.org/x/oauth2 library, but you can always use any other library that provides an http.Client. Access to the API requires OAuth client credentials from a Google developers project. This project must have the Library API enabled as described in https://developers.google.com/photos/library/guides/get-started.
import "golang.org/x/oauth2" func main() { ctx := context.Background() oc := oauth2Config := oauth2.Config{ ClientID: "... your application Client ID ...", ClientSecret: "... your application Client Secret ...", Endpoint: photos.Endpoint, Scopes: photos.Scopes, } tc := oc.Client(ctx, "... your user Oauth Token ...") client := gphotos.NewClient(tc) // look for a Google Photos Album by name album, _, err := client.AlbumByName(ctx, "my-album") }
Note that when using an authenticated Client, all calls made by the client will include the specified OAuth token. Therefore, authenticated clients should almost never be shared between different users. See the oauth2 docs for complete instructions on using that library.
Rate Limiting Google Photos imposes a rate limit on all API clients. The quota limit for requests to the Library API is 10,000 requests per project per day. The quota limit for requests to access media bytes (by loading a photo or video from a base URL) is 75,000 requests per project per day.
Photo storage and quality All media items uploaded to Google Photos using the API are stored in full resolution at original quality (https://support.google.com/photos/answer/6220791). They count toward the user’s storage.
Index ¶
- Variables
- func NewOAuthConfig(creds APIAppCredentials) *oauth2.Config
- func WithLogger(l log.Logger) func(*Client)
- type APIAppCredentials
- type Client
- func (c *Client) AddMediaItem(ctx context.Context, filename, albumID string) (*photoslibrary.MediaItem, error)
- func (c *Client) AlbumById(ctx context.Context, id string) (album *photoslibrary.Album, err error)
- func (c *Client) AlbumByName(name string) (album *photoslibrary.Album, found bool, err error)
- func (c *Client) GetOrCreateAlbumByName(name string) (*photoslibrary.Album, error)
- func (c *Client) MediaItemsByAlbum(ctx context.Context, album *photoslibrary.Album, limit int) (items []*photoslibrary.MediaItem, err error)
- func (c *Client) UploadFile(filename string, pAlbumID ...string) (*photoslibrary.MediaItem, error)
- func (c *Client) UploadFileResumable(filePath string, uploadURL *string, pAlbumID ...string) (*photoslibrary.MediaItem, error)
- type MemoryUploadSessionStore
- type Option
- type UploadSessionStore
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAlbumNotFound represents a failure to find the album. ErrAlbumNotFound = errors.New("specified album was not found") )
Functions ¶
func NewOAuthConfig ¶
func NewOAuthConfig(creds APIAppCredentials) *oauth2.Config
NewOAuthConfig returns the OAuth configuration for Google Photos service.
func WithLogger ¶
WithLogger set a new Logger to log messages.
Types ¶
type APIAppCredentials ¶
APIAppCredentials represents the credentials for Google Photos OAuth client.
type Client ¶
type Client struct { // Google Photos client *photoslibrary.Service // contains filtered or unexported fields }
Client is a client for uploading a media. photoslibrary does not provide `/v1/uploads` API so we implement here.
func NewClientWithOptions ¶
func NewClientWithOptions(httpClient *http.Client, store UploadSessionStore, options ...Option) (*Client, error)
NewClientWithOptions constructs a new gphotos.Client from the provided HTTP client and the given options.
`httpClient` is an client with authentication credentials. `store` is an UploadSessionStore to keep upload sessions to resume uploads.
func (*Client) AddMediaItem ¶
func (c *Client) AddMediaItem(ctx context.Context, filename, albumID string) (*photoslibrary.MediaItem, error)
AddMediaItem returns MediaItem created after uploading `filename` and adding it to `albumID`.
func (*Client) AlbumByName ¶
AlbumByName returns the album which match with the specified name.
NOTE: We are maintaining backwards compatibility, but `found` should be DEPRECATED and returning an error (gphotos.ErrAlbumNotFound) instead of it. (TODO)
func (*Client) GetOrCreateAlbumByName ¶
GetOrCreateAlbumByName returns an Album with the specified album name. If the album doesn't exists it will try to create it.
func (*Client) MediaItemsByAlbum ¶
func (*Client) UploadFile ¶
UploadFile actually uploads the media and activates it on google photos DEPRECATED: Use c.AddMediaItem(...) instead
type MemoryUploadSessionStore ¶
type MemoryUploadSessionStore struct {
// contains filtered or unexported fields
}
func (MemoryUploadSessionStore) Delete ¶
func (m MemoryUploadSessionStore) Delete(fingerprint string)
func (MemoryUploadSessionStore) Get ¶
func (m MemoryUploadSessionStore) Get(fingerprint string) []byte
func (MemoryUploadSessionStore) Set ¶
func (m MemoryUploadSessionStore) Set(fingerprint string, url []byte)
type UploadSessionStore ¶
type UploadSessionStore uploader.UploadSessionStore