Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 35471d0

Browse files
alexander-fensterJustinBeckwith
authored andcommitted
feat: use X-Goog-Api-Key header (#719)
1 parent 3318bff commit 35471d0

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@types/mocha": "^5.2.1",
3838
"@types/mv": "^2.1.0",
3939
"@types/ncp": "^2.0.1",
40-
"@types/nock": "^10.0.0",
40+
"@types/nock": "^10.0.3",
4141
"@types/node": "^10.5.1",
4242
"@types/semver": "^6.0.0",
4343
"@types/sinon": "^7.0.0",

src/auth/oauth2client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ export class OAuth2Client extends AuthClient {
756756
}
757757

758758
if (this.apiKey) {
759-
return {headers: {}};
759+
return {headers: {'X-Goog-Api-Key': this.apiKey}};
760760
}
761761
let r: GetTokenResponse | null = null;
762762
let tokens: Credentials | null = null;
@@ -885,9 +885,9 @@ export class OAuth2Client extends AuthClient {
885885
opts.headers = opts.headers || {};
886886
opts.headers.Authorization = r.headers.Authorization;
887887
}
888-
889888
if (this.apiKey) {
890-
opts.params = Object.assign(opts.params || {}, {key: this.apiKey});
889+
opts.headers = opts.headers || {};
890+
opts.headers['X-Goog-Api-Key'] = this.apiKey;
891891
}
892892
r2 = await this.transporter.request<T>(opts);
893893
} catch (e) {

test/test.googleauth.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,8 @@ describe('googleauth', () => {
242242
it('should make a request with the api key', async () => {
243243
const scope = nock(BASE_URL)
244244
.post(ENDPOINT)
245-
.query({key: API_KEY})
246-
.reply(uri => {
247-
assert(uri.indexOf('key=' + API_KEY) > -1);
245+
.reply(function(uri) {
246+
assert.strictEqual(this.req.headers['x-goog-api-key'][0], API_KEY);
248247
return [200, RESPONSE_BODY];
249248
});
250249
const client = auth.fromAPIKey(API_KEY);
@@ -257,13 +256,19 @@ describe('googleauth', () => {
257256
scope.done();
258257
});
259258

259+
it('should put the api key in the headers', async () => {
260+
const client = auth.fromAPIKey(API_KEY);
261+
const headers = await client.getRequestHeaders();
262+
assert.strictEqual(headers['X-Goog-Api-Key'], API_KEY);
263+
});
264+
260265
it('should make a request while preserving original parameters', async () => {
261266
const OTHER_QS_PARAM = {test: 'abc'};
262267
const scope = nock(BASE_URL)
263268
.post(ENDPOINT)
264-
.query({test: OTHER_QS_PARAM.test, key: API_KEY})
265-
.reply(uri => {
266-
assert(uri.indexOf('key=' + API_KEY) > -1);
269+
.query({test: OTHER_QS_PARAM.test})
270+
.reply(function(uri) {
271+
assert.strictEqual(this.req.headers['x-goog-api-key'][0], API_KEY);
267272
assert(uri.indexOf('test=' + OTHER_QS_PARAM.test) > -1);
268273
return [200, RESPONSE_BODY];
269274
});

0 commit comments

Comments
 (0)