I am creating this to see if it would be useful for anyone, as I have run into an instance where it would be nice (for testing). We have removed the accessToken option from our client libraries, so if you have a static access token, there's no way for you to easily provide it in order to authenticate. This may be by design, as it's better to set up appropriate credentials flows instead. However, we could add a Credential class like this for dev/testing:
use Google\Auth\FetchAuthTokenInterface;
class StaticTokenCredentials implements FetchAuthTokenInterface
{
private string $accessToken;
public function __construct(string $accessToken)
{
$this->accessToken = $accessToken;
}
/**
* Return the supplied access token.
*/
public function fetchAuthToken(callable $httpHandler = null): array
{
return [
'access_token' => $this->accessToken,
'expires_in' => 3600, // Customize if you know the exact TTL
];
}
public function getCacheKey(): ?string
{
// Unique key to prevent collision if tokens change
return 'static_token_' . md5($this->accessToken);
}
public function getLastReceivedToken(): ?array
{
return [
'access_token' => $this->accessToken,
];
}
}
I am creating this to see if it would be useful for anyone, as I have run into an instance where it would be nice (for testing). We have removed the
accessTokenoption from our client libraries, so if you have a static access token, there's no way for you to easily provide it in order to authenticate. This may be by design, as it's better to set up appropriate credentials flows instead. However, we could add a Credential class like this for dev/testing: