Skip to content

Commit

Permalink
Merge pull request #53 from burcu/no-token-fix
Browse files Browse the repository at this point in the history
OAuth2Client shouldn't throw access token error until attempting to refresh
  • Loading branch information
Burcu Dogan committed Jun 24, 2013
2 parents 8125740 + e5c0e1c commit d826c3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/auth/oauth2client.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ OAuth2Client.prototype.request =
var that = this;
var credentials = this.credentials;

if (!credentials.access_token) {
throw new Error('No access token is set.');
if (!credentials.access_token && !credentials.refresh_token) {
throw new Error('No access or refresh token is set.');
}

credentials.token_type = credentials.token_type || 'Bearer';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "googleapis",
"version": "0.2.11-alpha",
"version": "0.2.12-alpha",
"author": "Google Inc.",
"description": "Google APIs Client Library for Node.js",
"contributors": [
Expand Down
39 changes: 29 additions & 10 deletions tests/test.oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('OAuth2 client', function() {
done();
});

it('should throw exception no access token is set before making ' +
it('should throw exception no access or refresh token is set before making ' +
'a request', function() {
var gapis = new googleapis.GoogleApis();
var oauth2client =
Expand All @@ -80,23 +80,42 @@ describe('OAuth2 client', function() {
.newRequest('dummy', {})
.withAuthClient(oauth2client)
.execute();
}, Error, 'No access token is set.');
}, Error, 'No access or refresh token is set.');
});
});

it('should not throw any exceptions if only refresh token is set',
function() {
var gapis = new googleapis.GoogleApis();
var oauth2client =
new googleapis.OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.credentials = { refresh_token: 'refresh_token' };
gapis.Transporter = urlshortenerDiscoveryTransporter;
gapis
.discover('urlshortener', 'v1')
.execute(function(err, client) {
assert.doesNotThrow(function() {
client
.urlshortener.url.list()
.withAuthClient(oauth2client)
.execute();
});
});
});

it('should set access token type to Bearer if none is set', function(done) {
var gapis = new googleapis.GoogleApis();
var oauth2client =
new googleapis.OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.credentials = { access_token: 'foo' };
oauth2client.credentials = { access_token: 'foo', refresh_token: '' };
gapis
.discover('urlshortener', 'v1')
.execute(function(err, client) {
var req = client.urlshortener.url.list().withAuthClient(oauth2client);
req.execute(function(err, result) {
assert.equal(oauth2client.credentials.token_type, 'Bearer');
done();
});
.discover('urlshortener', 'v1')
.execute(function(err, client) {
var req = client.urlshortener.url.list().withAuthClient(oauth2client);
req.execute(function(err, result) {
assert.equal(oauth2client.credentials.token_type, 'Bearer');
done();
});
});
});

Expand Down

0 comments on commit d826c3a

Please sign in to comment.