Skip to content

Commit

Permalink
Add scopes and documentation (#2)
Browse files Browse the repository at this point in the history
* Add scopes constants and documentation

* Bump API version
  • Loading branch information
roopakv authored Oct 15, 2018
1 parent e709899 commit f1c7135
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
58 changes: 56 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Wrapper around the google photos API. The API reference can be found [here](http
## Getting Started

Construct an object with the google auth token. All actions performed on this instance of photos
will use the auth token the object was constructed with. This lib doesn't yet take care of getting
the auth token with the required scopes (coming soon).
will use the auth token the object was constructed with. Read the section below on getting an authtoken
with the required scopes.

```
const Photos = require('googlephotos');
Expand All @@ -16,6 +16,60 @@ const Photos = require('googlephotos');
const photos = new Photos(your_google_auth_token);
```

### Authentication

This package doesn't authentication itself. We suggest using the official
[google nodejs library](https://www.npmjs.com/package/googleapis). Here are their [instructions](https://www.npmjs.com/package/googleapis#oauth2-client).

Use the library to get the auth token for the scopes you will need. Read [this](https://developers.google.com/photos/library/guides/authentication-authorization) to figure out what
scopes you will need.

The scopes are available on the `Photos` object to make your life easier.

| Quick access | Scope | Use |
| --------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `Photos.Scopes.READ_ONLY` | https://www.googleapis.com/auth/photoslibrary.readonly | Only reading information. Sharing information is returned only if the token has sharing scope as well. |
| `Photos.Scopes.APPEND_ONLY` | https://www.googleapis.com/auth/photoslibrary.appendonly | Only add photos, create albums in the user's collection. No sort of read access. |
| `Photos.Scopes.READ_DEV_DATA` | https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata | Read access to media items and albums created by the developer. Use this with write only. |
| `Photos.Scopes.READ_AND_APPEND` | https://www.googleapis.com/auth/photoslibrary | Access to read and write only. No sharing information can be accessed. |
| `Photos.Scopes.SHARING | https://www.googleapis.com/auth/photoslibrary.sharing | Access to sharing information. |

You can figure out your client id, secret and redirect url by going to the
[Google Cloud Console](https://console.developers.google.com/apis/credentials) and navigating to
APIs -> Credentials.

```
const {google} = require('googleapis');
const Photos = require('googlephotos');
const oauth2Client = new google.auth.OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
const scopes = [
Photos.Scopes.READ_ONLY,
Photos.Scopes.SHARING,
];
const url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
// If you only need one scope you can pass it as a string
scope: scopes
});
// Send the user to the url from above. Once they grant access they will be redirected to the
// the redirect URL above with a query param code in the redirect. Use the code below to get the
// access token.
const {tokens} = await oauth2Client.getToken(code)
// The token from above can be used to initialize the photos library.
```

## Albums

### list
Expand Down
15 changes: 15 additions & 0 deletions constants/scopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/**
* Read more about scopes here:
* https://developers.google.com/photos/library/guides/authentication-authorization
*/
const SCOPES = {
READ_ONLY: 'https://www.googleapis.com/auth/photoslibrary.readonly',
APPEND_ONLY: 'https://www.googleapis.com/auth/photoslibrary.appendonly',
READ_DEV_DATA: 'https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata',
READ_AND_APPEND: 'https://www.googleapis.com/auth/photoslibrary',
SHARING: 'https://www.googleapis.com/auth/photoslibrary.sharing'
};

module.exports = SCOPES;
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const AlbumPosition = require('./albums/album_position');
const Transport = require('./transport');

const {CONTENT_CATEGORY, MEDIA_TYPE} = require('../constants/media_items');
const SCOPES = require('../constants/scopes');
const DateFilter = require('./common/date_filter');
const ContentFilter = require('./media_items/content_filter');
const MediaTypeFilter = require('./media_items/media_type_filter');
Expand Down Expand Up @@ -39,4 +40,6 @@ class Photos {
}
}

Photos.Scopes = SCOPES;

module.exports = Photos;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "googlephotos",
"version": "0.1.2",
"description": "Library to make hitting the Google Photos API easy",
"version": "0.1.3",
"description": "Library to make working with the Google Photos API easy",
"main": "lib/index.js",
"scripts": {
"test": "mocha test/**/*.js",
Expand Down

0 comments on commit f1c7135

Please sign in to comment.