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/gphotosuploader/googlemirror.
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) AlbumByName(name string) (album *photoslibrary.Album, found bool, err error)
- func (c *Client) GetOrCreateAlbumByName(name string) (*photoslibrary.Album, error)
- func (c *Client) Token() *oauth2.Token
- 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 Option
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 ¶ added in v1.1.3
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 NewClient ¶
NewClient constructs a new PhotosClient from an oauth httpClient.
`httpClient` is an HTTP Client with authentication credentials.
DEPRECATED: Use NewClientWithOptions(...) instead. This package doesn't need Client.token anymore, used `Client.Client` instead.
func NewClientWithResumableUploads ¶ added in v1.1.0
func NewClientWithResumableUploads(httpClient *http.Client, store uploader.UploadSessionStore, options ...Option) (*Client, error)
NewClientWithResumableUploads 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 ¶ added in v1.1.0
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) Token ¶
Token returns the value of the token used by the gphotos Client Cannot be used to set the token
DEPRECATED: Use the authenticated HTTP Client `Client.Client` instead.
func (*Client) UploadFile ¶
UploadFile actually uploads the media and activates it on google photos DEPRECATED: Use c.AddMediaItem(...) instead
func (*Client) UploadFileResumable ¶ added in v1.0.3
func (c *Client) UploadFileResumable(filePath string, uploadURL *string, pAlbumID ...string) (*photoslibrary.MediaItem, error)
UploadFileResumable return a MediaItem created after uploding the specified file DEPRECATED: Use c.UploadFile(...) instead.