oauth2-client icon indicating copy to clipboard operation
oauth2-client copied to clipboard

Set custom User-Agent for requests

Open abodnar opened this issue 6 years ago • 4 comments

I'm using a GenericProvider and when my application attempts to request the access token from my company's SSO, it hits an error that it's been blocked. Upon further investigation with the SSO team, they determined that this is due to the application being hosted in AWS and has a generic User-Agent, GuzzleHTTP/ Curl/ PHP/.

Is there a way to set a custom User-Agent for the getAccessToken request? Looking at getAccessTokenOptions method, looks like you can only set the body and can't change any other headers.

Do I need to make my own provider? Seems a bit heavy to do for just a small thing piece that would useful to be able to customize?

abodnar avatar Mar 20 '18 22:03 abodnar

This is still very relevant as Apple Signin requires a proper User-Agent to be passed.

isakrubin avatar Aug 28 '19 07:08 isakrubin

Any updates on this? I'm facing this same issue myself with Basecamp's API.

sabs21 avatar Jun 19 '20 18:06 sabs21

Possibly related to or solved through #863

ramsey avatar Oct 28 '20 20:10 ramsey

(I posted this on the wrong issue, now moved here)

The solution I have found, using the Xero provider, is to create my own Guzzle instance outside the provider.

So instead of this:

        $provider = new \Calcinai\OAuth2\Client\Provider\Xero([
            'clientId' => 'my-client-id',
            'clientSecret' => 'my-client-secret',
        ]};

I have this:

        $httpClient = new \GuzzleHttp\Client([
            'headers' => [
                'User-Agent' => 'My Xero Application name',
            ],
        ]);

        $provider = new \Calcinai\OAuth2\Client\Provider\Xero([
            'clientId' => 'my-client-id',
            'clientSecret' => 'my-client-secret',
        ], [
            'httpClient' => $httpClient,
        ]};

Even though the message is a PSR-7 message that should contain everything, Guzzle still slips in some headers on top of that, and the User-Agent is one of them. Xero in particular is very keen on the correct User-Agent being used, to help with support issues that may arise.

Note that these header parameters are for Guzzle 7. The format is slightly different on Guzzle 6 (wrapped in a defaults array) and different again on Guzzle 5.

judgej avatar Feb 15 '21 18:02 judgej