Procédure pas à pas : Application web DE l’API Bing Ads en PHP

Cet exemple d’application web PHP demande le consentement de l’utilisateur via les informations d’identification que vous fournissez, puis obtient les comptes auxquels l’utilisateur authentifié peut accéder.

Vous devez d’abord inscrire une application et prendre note de l’ID client (ID d’application inscrit), de la clé secrète client (mot de passe inscrit) et de l’URI de redirection. Pour plus d’informations sur l’inscription d’une application et le flux d’octroi du code d’autorisation, consultez Authentification avec OAuth.

Vous aurez également besoin de votre jeton de développeur de production. Vous pouvez créer l’exemple étape par étape comme décrit ci-dessous ou télécharger d’autres exemples à partir de GitHub.

Exemple d’authentification d’application web Walk-Through

  1. Créez un fichier nommé Index.php et ajoutez le code comme indiqué dans l’exemple suivant.

    <?php
    namespace Microsoft\BingAds\Samples;
    
    require_once "./vendor/autoload.php";
    
    include "WebAuthHelper.php";
    
    // Specify the Microsoft\BingAds\Samples classes that will be used.
    use Microsoft\BingAds\Samples\WebAuthHelper;
    
    ?>
    
    <!-- 
    Display a small form with a login button. In your application, you would implement code to allow
    the user to either log into your service or create a new account. 
    -->
    <h2>Microsoft Advertising OAuth Demo</h2>
    
    <p>This application would like to manage your Microsoft Advertising data. Click below to login and authorize this application.</p>
    
    <p>When you click OK below, you'll be redirected to the following URI:</p>
    <p><?php echo WebAuthHelper::RedirectUri ?></p>
    
    <!-- 
    When the user presses this button, they will be redirected to fully formed URL to request an authorization token. 
    From here, the user will be redirected to the RedirectUri where the authorization token can be extracted. 
    -->
    <input type="button" onClick="return window.location='<?php echo WebAuthHelper::RedirectUri;?>';" value="OK" />
    
  2. Créez un fichier nommé OAuth2Callback.php et ajoutez le code comme indiqué dans l’exemple suivant.

    <?php
    namespace Microsoft\BingAds\Samples;
    
    require_once "./vendor/autoload.php";
    
    include "WebAuthHelper.php";
    
    // Specify the Microsoft\BingAds\Auth classes that will be used.
    use Microsoft\BingAds\Auth\AuthorizationData;
    use Microsoft\BingAds\Auth\OAuthTokenRequestException;
    use Microsoft\BingAds\Auth\OAuthWebAuthCodeGrant;
    
    // Specify the Microsoft\BingAds\Samples classes that will be used.
    use Microsoft\BingAds\Samples\WebAuthHelper;
    
    use Exception;
    
    try 
    {
        session_start();
    
        if(!isset($_SESSION['AuthorizationData']) || !isset($_SESSION['AuthorizationData']->Authentication))
        {
            // Prepare the OAuth object for use with the authorization code grant flow. 
            // It is recommended that you specify a non guessable 'state' request parameter to help prevent
            // cross site request forgery (CSRF). 
            $authentication = (new OAuthWebAuthCodeGrant())
                ->withClientId(WebAuthHelper::ClientId)
                ->withClientSecret(WebAuthHelper::ClientSecret)
                ->withRedirectUri('https://' . $_SERVER['HTTP_HOST'] . WebAuthHelper::RedirectUri)
                ->withState(rand(0,999999999)); 
    
            // Assign this authentication instance to the global authorization_data. 
    
            $_SESSION['AuthorizationData'] = (new AuthorizationData())
                ->withAuthentication($authentication)
                ->withDeveloperToken(WebAuthHelper::DeveloperToken);
    
            $_SESSION['state'] = $_SESSION['AuthorizationData']->Authentication->State;
    
            // The user needs to provide consent for the application to access their Microsoft Advertising accounts.
            header('Location: '. $_SESSION['AuthorizationData']->Authentication->GetAuthorizationEndpoint());
        }
    
        // If the current HTTP request is a callback from the Microsoft Account authorization server,
        // use the current request url containing authorization code to request new access and refresh tokens
        if(isset($_GET['code']))
        {   
            // Verify whether the 'state' value is the same random value we created
            // when the authorization request was initiated.
            if ($_GET['state'] != $_SESSION['state'])
            {
                throw new Exception(sprintf(
                    "The OAuth response state (%s) does not match the client request state (%s)", 
                    $_GET['state'], 
                    $_SESSION['state']));
            }   
    
            $_SESSION['AuthorizationData']->Authentication->RequestOAuthTokensByResponseUri(
                $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    
            header('Location: '. '/CallBingAdsServices.php');
        }
    }
    catch(OAuthTokenRequestException $e)
    {
        // The user could not be authenticated or the grant is expired. 
        // The user must first sign in and if needed grant the client application access to the requested scope.
        print $e;
    }
    catch(Exception $e)
    {
        print $e;
    }    
    
  3. Créez un fichier nommé CallBingAdsServices.php et ajoutez le code comme indiqué dans l’exemple suivant.

    <?php
    namespace Microsoft\BingAds\Samples;
    
    require_once "./vendor/autoload.php";
    
    include "WebAuthHelper.php";
    
    // Specify the Microsoft\BingAds\Auth classes that will be used.
    use Microsoft\BingAds\Auth\AuthorizationData;
    use Microsoft\BingAds\Auth\ServiceClient;
    use Microsoft\BingAds\Auth\ServiceClientType;
    
    // Specify the Microsoft\BingAds\V13\CustomerManagement classes that will be used.
    use Microsoft\BingAds\V13\CustomerManagement\GetUserRequest;
    use Microsoft\BingAds\V13\CustomerManagement\SearchAccountsRequest;
    use Microsoft\BingAds\V13\CustomerManagement\Paging;
    use Microsoft\BingAds\V13\CustomerManagement\Predicate;
    use Microsoft\BingAds\V13\CustomerManagement\PredicateOperator;
    
    // Specify the Microsoft\BingAds\Samples classes that will be used.
    use Microsoft\BingAds\Samples\WebAuthHelper;
    
    use Exception;
    
    session_start();
    
    // If there is no user authenticated, go back to the site index.
    
    if(!isset($_SESSION['AuthorizationData']) || 
    !isset($_SESSION['AuthorizationData']->Authentication) || 
    !isset($_SESSION['AuthorizationData']->Authentication->OAuthTokens)
    )
    {
        header('Location: '. 'https://' . $_SERVER['HTTP_HOST']);
    }
    else {
        // If a refresh token is already present, use it to request new access and refresh tokens.
        // You should store refresh tokens securely i.e. not in session as shown in this demo.
    
        $refreshToken = $_SESSION['AuthorizationData']->Authentication->OAuthTokens->RefreshToken;
        if($refreshToken != null)
        {
            $_SESSION['AuthorizationData']->Authentication->RequestOAuthTokensByRefreshToken($refreshToken);
        }
    
        printf("Access token: %s<br/>", $_SESSION['AuthorizationData']->Authentication->OAuthTokens->AccessToken);
        printf("Refresh token: %s<br/>", $_SESSION['AuthorizationData']->Authentication->OAuthTokens->RefreshToken);
    
        $GLOBALS['CustomerManagementProxy'] = new ServiceClient(
            ServiceClientType::CustomerManagementVersion13, 
            $_SESSION['AuthorizationData'], 
            WebAuthHelper::GetApiEnvironment());
    
        // Set the GetUser request parameter to an empty user identifier to get the current 
        // authenticated Microsoft Advertising user, and then search for all accounts the user may access.
    
        $getUserRequest = new GetUserRequest();
        $getUserRequest->UserId = null;
    
        $user = $GLOBALS['CustomerManagementProxy']->GetService()->GetUser($getUserRequest)->User;
    
        // Search for the Microsoft Advertising accounts that the user can access.
    
        $pageInfo = new Paging();
        $pageInfo->Index = 0;    // The first page
        $pageInfo->Size = 1000;   // The first 1,000 accounts for this page of results    
        $predicate = new Predicate();
        $predicate->Field = "UserId";
        $predicate->Operator = PredicateOperator::Equals;
        $predicate->Value = $user->Id; 
    
        $searchAccountsRequest = new SearchAccountsRequest();
        $searchAccountsRequest->Predicates = array($predicate);
        $searchAccountsRequest->Ordering = null;
        $searchAccountsRequest->PageInfo = $pageInfo;
    
        $accounts = $GLOBALS['CustomerManagementProxy']->GetService()->SearchAccounts($searchAccountsRequest)->Accounts;
    
        print "-----<br/>Accounts the user can access:<br/>";
        foreach ($accounts->AdvertiserAccount as $account)
        {
            printf("Account Name: %s<br/>", $account->Name);
        }
    }
    
    session_unset();
    
    ?>
    
  4. Créez un fichier nommé WebAuthHelper.php et ajoutez le code comme indiqué dans l’exemple suivant. Vous devez modifier l’exemple ci-dessous avec les valeurs ClientId, ClientSecret et RedirectionUri qui ont été provisionnées lors de l’inscription de votre application. Vous devez également modifier l’exemple avec votre jeton de développeur de production.

    <?php
    
    namespace Microsoft\BingAds\Samples;
    
    require_once "./vendor/autoload.php";
    
    // Specify the Microsoft\BingAds\Auth classes that will be used.
    use Microsoft\BingAds\Auth\ApiEnvironment;
    
    /** 
    * Defines global settings that you can use for testing your application.
    * Your production implementation may vary, and you should always store sensitive information securely.
    */
    final class WebAuthHelper {
    
        const DeveloperToken = 'DeveloperTokenGoesHere';
        const ApiEnvironment = ApiEnvironment::Production;
        const ClientId = 'ClientIdGoesHere'; 
        const ClientSecret = 'ClientSecretGoesHere'; 
        const RedirectUri = '/OAuth2Callback.php'; 
    
        static function GetApiEnvironment() 
        {
            return WebAuthHelper::ApiEnvironment;
        }
    }
    
  5. L’application est prête à être déployée sur un serveur avec les bibliothèques clientes PHP installées . Par exemple, vous pouvez publier une application web à l’aide de la Azure App Service. Pour plus d’informations, consultez Configurer PHP dans Azure App Service Web Apps.

Voir aussi

Exemples de code de l’API Bing Ads
Adresses du service web de l’API Bing Ads