Created
November 1, 2024 16:47
-
-
Save josephdpurcell/b434efc4870e4e7ba8387f9f7b98fd47 to your computer and use it in GitHub Desktop.
google-auth-library-nodejs: Default User-Agent header is overwritten by DefaultTransporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AuthClient, IdTokenClient } from 'google-auth-library'; | |
const googleAuth = new GoogleAuth(); | |
const idTokenClient = await googleAuth.getIdTokenClient('https://www.example.com/'); | |
idTokenClient.gaxios.defaults.headers = { | |
'User-Agent': 'foo/1', | |
}; | |
idTokenClient.request({ | |
method: 'GET', | |
url: 'https://www.example.com/', | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AuthClient, DefaultTransporter, IdTokenClient } from 'google-auth-library'; | |
const googleAuth = new GoogleAuth(); | |
const idTokenClient = await googleAuth.getIdTokenClient('https://www.example.com/'); | |
export class CustomTransporter extends DefaultTransporter { | |
configure(opts?: GaxiosOptions | undefined): GaxiosOptions { | |
const options = super.configure(opts); | |
// If we have a default then overwrite the value. | |
if (this.defaults.headers?.['User-Agent']) { | |
if (!options.headers) { | |
options.headers = {}; | |
} | |
options.headers['User-Agent'] = this.defaults.headers['User-Agent']; | |
} | |
return options; | |
} | |
} | |
idTokenClient.transporter = new CustomTransporter(); | |
idTokenClient.gaxios.defaults.headers = { | |
'User-Agent': 'foo/1', | |
}; | |
idTokenClient.request({ | |
method: 'GET', | |
url: 'https://www.example.com/', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment