Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Ads Api Oauth Catchable Exception #404

Open
necrogami opened this issue May 17, 2022 · 3 comments
Open

Google Ads Api Oauth Catchable Exception #404

necrogami opened this issue May 17, 2022 · 3 comments
Labels
needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.

Comments

@necrogami
Copy link

        try {
            // Generate a refreshable OAuth2 credential for authentication.
            $oAuth2Credential = (new OAuth2TokenBuilder())
                ->withClientId(env('GOOGLE_ADS_CLIENT_ID'))
                ->withClientSecret(env('GOOGLE_ADS_CLIENT_SECRET'))
                ->withRefreshToken($refreshToken)
                ->build();
        } catch (\Throwable $e) {
            return $this->job->fail();
        }

        try {
            // Construct a Google Ads client configured from a properties file and the
            // OAuth2 credentials above.
            $googleAdsClient = (new GoogleAdsClientBuilder())
                ->withOAuth2Credential($oAuth2Credential)
                ->withDeveloperToken(env('GOOGLE_ADS_DEVELOPER_TOKEN'))
                ->withLoginCustomerId($customerID)
                ->withTransport('rest')
                ->build();
        } catch (\Throwable $e) {
            return $this->job->fail();
        }

So i have the code above which is using the google auth library to create the oauth credential, however some of our clients have expired refresh tokens: This spits out:

Message
Client error: POST https://oauth2.googleapis.com/token resulted in a 400 Bad Request response:
{
 "error": "invalid_grant",
 "error_description": "Token has been expired or revoked."
}
Level
ERROR
Exception
{
    "class": "GuzzleHttp\\Exception\\ClientException",
    "message": "Client error: `POST https://oauth2.googleapis.com/token` resulted in a `400 Bad Request` response:\n{\n  \"error\": \"invalid_grant\",\n  \"error_description\": \"Token has been expired or revoked.\"\n}\n",
    "code": 400,
    "file": "/usr/local/bigtop/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113",
    "trace": [
        "/usr/local/bigtop/vendor/guzzlehttp/guzzle/src/Middleware.php:69",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:204",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:153",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/TaskQueue.php:48",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:248",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:224",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:269",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:226",
        "/usr/local/bigtop/vendor/guzzlehttp/promises/src/Promise.php:62",
        "/usr/local/bigtop/vendor/guzzlehttp/guzzle/src/Client.php:123",
        "/usr/local/bigtop/vendor/google/auth/src/HttpHandler/Guzzle6HttpHandler.php:47",
        "/usr/local/bigtop/vendor/google/auth/src/OAuth2.php:544",
        "/usr/local/bigtop/vendor/google/auth/src/Credentials/UserRefreshCredentials.php:114",
        "/usr/local/bigtop/vendor/google/auth/src/CredentialsLoader.php:214",
        "/usr/local/bigtop/vendor/google/gax/src/CredentialsWrapper.php:208",
        "/usr/local/bigtop/vendor/google/gax/src/Transport/HttpUnaryTransportTrait.php:111",
        "/usr/local/bigtop/vendor/google/gax/src/Transport/RestTransport.php:110",
        "/usr/local/bigtop/vendor/google/gax/src/GapicClientTrait.php:608",
        "/usr/local/bigtop/vendor/google/gax/src/Middleware/CredentialsWrapperMiddleware.php:61",
        "/usr/local/bigtop/vendor/google/gax/src/Middleware/FixedH

Is there a good way to be able to catch that since it's being caught inside the libraries and i'm not seeing a way to properly handle that exception.

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels May 18, 2022
@necrogami
Copy link
Author

This has been open for over a half a month, I've been told to post here by both the googleads api php library team and google ads api forums.

Is there anyone who can help me with this?

@bshaffer
Copy link
Contributor

Hi @necrogami!

Do you know what is causing your refresh_tokens to be revoked? Refresh tokens do not expire.

As far as an elegant way to solve this, you could probably add a middleware to the GoogleAdsClient, but that would be out of the scope of this auth library.

@bshaffer bshaffer added type: question Request for information or clarification. Not an issue. needs more info This issue needs more information from the customer to proceed. labels Jun 17, 2022
@yoshi-automation yoshi-automation removed triage me I really want to be triaged. 🚨 This issue needs some love. labels Jun 17, 2022
@necrogami
Copy link
Author

We have a lot of customers and it's possible someone inside the company replaced the token without updating the token in our system. This happens in the auth layer and not the google ads client, i've already been told by the ads api team that this is an auth issue and they closed my corresponding ticket. googleads/google-ads-php#803

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants