BingAds / BingAds-PHP-SDK

Other
56 stars 46 forks source link

ReportRequest fails in deserialization #159

Closed sophie-kuehn closed 3 years ago

sophie-kuehn commented 3 years ago

Hi. I try to follow the example of https://github.com/BingAds/BingAds-PHP-SDK/blob/main/samples/V13/ReportRequests.php to prototype a report download. I got myself authorized but now receive an exception trying to execute SubmitGenerateReport(). So far i followed the example. What am i doing wrong here? I'm inexperienced with SOAP and the Bing Ads SDK.

Exception:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter https://bingads.microsoft.com/Reporting/v13:ReportRequest. The InnerException message was 'There was an error deserializing the object of type Microsoft.AdCenter.Advertiser.Reporting.Api.DataContracts.Request.ReportRequest. The value '' cannot be parsed as the type 'Int64'.'.  Please see InnerException for more details.

Code:

function GetAccountPerformanceReportRequest($accountId, $reportingProxy)
{
    $report = new \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportRequest();

    $report->Format = \Microsoft\BingAds\V13\Reporting\ReportFormat::Tsv;
    $report->ReportName = 'My Account Performance Report';
    $report->ReturnOnlyCompleteData = false;
    $report->Aggregation = \Microsoft\BingAds\V13\Reporting\ReportAggregation::Weekly;

    $report->Scope = new \Microsoft\BingAds\V13\Reporting\AccountReportScope();
    $report->Scope->AccountIds = array();
    $report->Scope->AccountIds[] = $accountId;

    //$report->Time->CustomDateRangeStart = null;
    //$report->Time->CustomDateRangeEnd = null;
    $report->Time = new \Microsoft\BingAds\V13\Reporting\ReportTime();
    $report->Time->PredefinedTime = \Microsoft\BingAds\V13\Reporting\ReportTimePeriod::Yesterday;

    $report->Columns = array (
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::TimePeriod,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::AccountId,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::AccountName,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::Clicks,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::Impressions,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::Ctr,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::AverageCpc,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::Spend,
        \Microsoft\BingAds\V13\Reporting\AccountPerformanceReportColumn::DeviceOS
    );

    $encodedReport = new SoapVar(
        $report,
        SOAP_ENC_OBJECT,
        'AccountPerformanceReportRequest',
        $reportingProxy->GetNamespace()
    );

    return $encodedReport;
}

$authentication = (new \Microsoft\BingAds\Auth\OAuthDesktopMobileAuthCodeGrant())
    ->withEnvironment(\Microsoft\BingAds\Auth\ApiEnvironment::Production)
    ->withClientId($clientId)
    ->withOAuthScope(\Microsoft\BingAds\Auth\OAuthScope::MSADS_MANAGE);

$authorizationData = (new \Microsoft\BingAds\Auth\AuthorizationData())
    ->withAuthentication($authentication)
    ->withDeveloperToken($developerToken);

if (empty($refreshToken)) {
    print "You need to provide consent for the application to access your Microsoft Advertising accounts. " .
        "Copy and paste this authorization endpoint into a web browser and sign in with a Microsoft account " .
        "with access to a Microsoft Advertising account: \n\n" . $authorizationData->Authentication->GetAuthorizationEndpoint() .
        "\n\nAfter you have granted consent in the web browser for the application to access your Microsoft Advertising accounts, " .
        "please enter the response URI that includes the authorization 'code' parameter: \n\n";

    $responseUri = fgets(STDIN);
    print "\n";

    $authorizationData->Authentication->RequestOAuthTokensByResponseUri(trim($responseUri));
} else {
    $authorizationData->Authentication->RequestOAuthTokensByRefreshToken($refreshToken);
}

$refreshToken = $cache['refreshToken'] =
    $authorizationData->Authentication->OAuthTokens->RefreshToken;

$reportingProxy = new \Microsoft\BingAds\Auth\ServiceClient(
    \Microsoft\BingAds\Auth\ServiceClientType::ReportingVersion13,
    $authorizationData,
    \Microsoft\BingAds\Auth\ApiEnvironment::Production
);

$request = new \Microsoft\BingAds\V13\Reporting\SubmitGenerateReportRequest();
$request->ReportRequest = GetAccountPerformanceReportRequest(
    $authorizationData->AccountId,
    $reportingProxy
);

$report = $reportingProxy->GetService()->SubmitGenerateReport($request);

A hint would be appreciated.

sophie-kuehn commented 3 years ago

I found my issue. Sorry for the noise :-)