BingAds / BingAds-PHP-SDK

Other
56 stars 46 forks source link

AuthHelper not working #31

Closed madsem closed 7 years ago

madsem commented 7 years ago

Hi,

after a good amount of testing I can't make even the connection to the API work anymore while trying to migrate to v11.

We're saving the refresh and access token in a DB.

According to the given example in AuthHelper I am trying to retrieve a new access token like:

static function AuthenticateWithOAuth()
    {
        $authentication = (new OAuthDesktopMobileAuthCodeGrant())
            ->withClientId(AuthHelper::ClientId)
            ->withRedirectUri(AuthHelper::RedirectUri);

        $GLOBALS['AuthorizationData'] = (new AuthorizationData())
            ->withAuthentication($authentication)
            ->withDeveloperToken(AuthHelper::DeveloperToken);

        try
        {
            // retrieve oAuth refresh_token
            // connect to db
            $getToken = new SqLite();
            $results = $getToken->returnWhere('oauth', 'id', 1);
            $refreshToken = $results[0]['refresh_token'];

            if($refreshToken != null)
            {
                $GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByRefreshToken($refreshToken);
                AuthHelper::WriteOAuthRefreshToken($GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken);
            }
            else
            {
                AuthHelper::RequestUserConsent();
            }

        }
        catch(OAuthTokenRequestException $e)
        {
            printf("Error: %s\n", $e->Error);
            printf("Description: %s\n", $e->Description);

            AuthHelper::RequestUserConsent();
        }
    }

But in the console I get:

ubuntu@xxxxxxx:~$ php /var/www/xxxxxxxx.local.vm/xxxxxxxxxxx.php
Error: invalid_client
Description: The provided request must include a 'client_secret' input parameter.
You need to provide consent for the application to access your Bing Ads accounts. Copy and paste this authorization endpoint into a web browser and sign in with a Microsoft account with access to a Bing Ads account: 

https://login.live.com/oauth20_authorize.srf?client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&scope=bingads.manage&response_type=code&redirect_uri=https://login.live.com/oauth20_desktop.srf

After you have granted consent in the web browser for the application to access your Bing Ads accounts, please enter the response URI that includes the authorization 'code' parameter: 
J7mbo commented 7 years ago

The error might be helpful... you're doing: ->withClientId(AuthHelper::ClientId) but I can't see any calls to ->withClientSecret() in your code...

eric-urban commented 7 years ago

@madsem - As @J7mbo inferred you would need to provide the client secret via ->withClientSecret() if you have registered a web app. If you are not using a web app you can edit the platform via https://apps.dev.microsoft.com/ and choose Native.