Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion dist/OAuth2.gs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,14 @@ Service_.prototype.hasAccess = function() {
if (token && !this.isExpired_(token)) return true; // Token still has access.
var canGetToken = (token && this.canRefresh_(token)) ||
this.privateKey_ || this.grantType_;
if (!canGetToken) return false;
if (!canGetToken) {
this.lastError_ = new Error('No token found and no way to generate one. ' +
'The user needs to be redirected to the authorization URL returned ' +
'by getAuthorizationUrl(), or the service needs to be configured ' +
'with setPrivateKey() or setGrantType() for a token to be ' +
'generated automatically.');
return false;
}

return this.lockable_(function() {
// Get the token again, bypassing the local memory cache.
Expand Down
9 changes: 8 additions & 1 deletion src/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,14 @@ Service_.prototype.hasAccess = function() {
if (token && !this.isExpired_(token)) return true; // Token still has access.
var canGetToken = (token && this.canRefresh_(token)) ||
this.privateKey_ || this.grantType_;
if (!canGetToken) return false;
if (!canGetToken) {
this.lastError_ = new Error('No token found and no way to generate one. ' +
'The user needs to be redirected to the authorization URL returned ' +
'by getAuthorizationUrl(), or the service needs to be configured ' +
'with setPrivateKey() or setGrantType() for a token to be ' +
'generated automatically.');
return false;
}

return this.lockable_(function() {
// Get the token again, bypassing the local memory cache.
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ describe('Service', () => {
service.hasAccess();
assert.equal(lock.counter, 0);
});

it('should set lastError_ when there is no token and no way to get one',
() => {
var properties = new MockProperties();
var service = OAuth2.createService('test')
.setClientId('abc')
.setClientSecret('def')
.setTokenUrl('http://www.example.com')
.setAuthorizationBaseUrl('http://www.example.com')
.setPropertyStore(properties);

var result = service.hasAccess();

assert.isFalse(result);
assert.exists(service.getLastError());
assert.match(service.getLastError().message, /no way to generate/);
});
});

describe('#refresh()', () => {
Expand Down