diff --git a/lib/auth/oauth2client.js b/lib/auth/oauth2client.js index 4566fdb168b..7f54d20292e 100644 --- a/lib/auth/oauth2client.js +++ b/lib/auth/oauth2client.js @@ -135,7 +135,7 @@ OAuth2Client.prototype.refreshAccessToken = function(callback) { var that = this; - if (! this.credentials.refresh_token){ + if (! this.credentials.refresh_token) { throw new Error('No refresh token is set'); } @@ -189,6 +189,14 @@ OAuth2Client.prototype.request = opts.headers['Authorization'] = credentials.token_type + ' ' + credentials.access_token; + // Make a reference copy of the multipart bodies + var bodies = []; + if (opts.multipart) { + opts.multipart.forEach(function(part) { + bodies.push(part.body); + }); + } + this.transporter.request(opts, function(err, body, res) { // TODO: Check if it's not userRateLimitExceeded var hasAuthError = res.statusCode == 401 || res.statusCode == 403; @@ -203,6 +211,12 @@ OAuth2Client.prototype.request = var tokens = result; tokens.refresh_token = credentials.refresh_token; that.credentials = tokens; + + // Refresh the bodies + bodies.forEach(function(body, i) { + opts.multipart[i].body = body; + }); + that.request(opts, opt_callback, true); } });