Question

I want to directly load data from the Bing Ads Api into the statistic tool R. I use the httr package within R, which uses Curl to enable a connection between R and the API. According to the API manual I try to SubmitGenerateReport, but I do not receive a ReportRequestId.

Here is the R Code:

#loads the httr package

library(httr)

#creates the statement, body of http soap object

`body <- '<s:Body>
             <SubmitGenerateReportRequestxmlns="https://bingads.microsoft.com/Reporting/v9">
    
<ReportRequest i:nil="false" i:type="KeywordPerformanceReportRequest">
    
<Format i:nil="false">Csv</Format>
    
<Language i:nil="false">English</Language>
    
<ReportName i:nil="false">Test</ReportName>
       <ReturnOnlyCompleteData>false</ReturnOnlyCompleteData>
    
<Aggregation>Daily</Aggregation>
    
<Columns>\
    
<KeywordPerformanceReportColumn>AccountName</KeywordPerformanceReportColumn>\
    <KeywordPerformanceReportColumn>CampaignName</KeywordPerformanceReportColumn>\
    <KeywordPerformanceReportColumn>Keyword</KeywordPerformanceReportColumn>\
    <KeywordPerformanceReportColumn>TimePeriod</KeywordPerformanceReportColumn>\
    <KeywordPerformanceReportColumn>Impressions</KeywordPerformanceReportColumn>\
    <KeywordPerformanceReportColumn>Conversions</KeywordPerformanceReportColumn>\
    
</Columns>\
    
<Scope>\

    <AccountIds>MyAccountId</AccountIds>\

    </Scope>\
    
<Time>\
    
<PredefinedTime>LastSevenDays</PredefinedTime>\
    
</Time>\
    
</ReportRequest>

    </SubmitGenerateReportRequest>
    
</s:Body>'`



# post the api request
response <- POST("https://bingads.microsoft.com/Reporting/v9",
     c(verbose(),add_headers("Action" = "SubmitGenerateReport",
                              "Content-Type" = "text/xml; charset=utf-8",
                              "CustomerAccountId"  =  "MyCustomerAccountId",
                              "CustomerId"  =  "MyCustomerId",
                              "DeveloperToken"  =  "MyDevToken",
                              "Password"  =  "MyPassword",
                              "UserName"  =  "MyUserName")),
     body = body)

The Api returns following:

* Adding handle: conn: 0x7fd10d167200
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fd10d167200) send_pipe: 1, recv_pipe: 0
* About to connect() to bingads.microsoft.com port 443 (#0)
*   Trying ***.**.***.***...
* Connected to bingads.microsoft.com (***.**.***.***) port 443 (#0)
* TLS 1.0 connection using TLS_RSA_WITH_RC4_128_SHA
* Server certificate: bingads.microsoft.com
* Server certificate: VeriSign Class 3 Extended Validation SSL SGC CA
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> POST /Reporting/v9 HTTP/1.1
Host: bingads.microsoft.com
Accept: */*
Accept-Encoding: gzip
user-agent: curl/7.30.0 Rcurl/1.95.4.1 httr/0.3
Action: SubmitGenerateReport
Content-Type: text/xml; charset=utf-8
CustomerAccountId: *******
CustomerId: ******
DeveloperToken: **************
Password: *********
UserName: ***********
Content-Length: 1056
Expect: 100-continue

< HTTP/1.1 302 Found
< Location: https://bingads.microsoft.com//default.aspx?reason=1&msg=sessionexpired&rurl=https%3a%2f%2fbingads.microsoft.com%2fReporting%2fv9
* Server Microsoft-IIS/8.0 is not blacklisted
< Server: Microsoft-IIS/8.0
< X-MiniProfiler-Ids: []
< X-Powered-By: ASP.NET
< p3p: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, OPTIONS
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
< Date: Tue, 29 Apr 2014 09:45:54 GMT
< Content-Length: 254
< 
* Closing connection 0
* Issue another request to this URL: 'https://bingads.microsoft.com//default.aspx?reason=1&msg=sessionexpired&rurl=https%3a%2f%2fbingads.microsoft.com%2fReporting%2fv9'
* About to connect() to bingads.microsoft.com port 443 (#1)
*   Trying ***.**.***.***...
* Adding handle: conn: 0x7fd10a685800
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 1 (0x7fd10a685800) send_pipe: 1, recv_pipe: 0
* Connected to bingads.microsoft.com (***.**.***.***) port 443 (#1)
* TLS 1.0 connection using TLS_RSA_WITH_RC4_128_SHA
* Server certificate: bingads.microsoft.com
* Server certificate: VeriSign Class 3 Extended Validation SSL SGC CA
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> POST //default.aspx?reason=1&msg=sessionexpired&rurl=https%3a%2f%2fbingads.microsoft.com%2fReporting%2fv9 HTTP/1.1
Host: bingads.microsoft.com
Accept: */*
Accept-Encoding: gzip
user-agent: curl/7.30.0 Rcurl/1.95.4.1 httr/0.3
Action: SubmitGenerateReport
Content-Type: text/xml; charset=utf-8
CustomerAccountId: ********
CustomerId: *********
DeveloperToken: ***************
Password: **********
UserName: ***********
Content-Length: 1056
Expect: 100-continue

< HTTP/1.1 302 Redirect
< Cache-Control: private, no-store
< Content-Type: text/html; charset=UTF-8
< Location: https://secure.bingads.microsoft.com/?reason=1&msg=sessionexpired&rurl=https%3a%2f%2fbingads.microsoft.com%2fReporting%2fv9
* Server Microsoft-IIS/8.0 is not blacklisted
< Server: Microsoft-IIS/8.0
< X-Powered-By: ASP.NET
< p3p: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Date: Tue, 29 Apr 2014 09:45:55 GMT
< Content-Length: 254
< 
* Closing connection 1
* Issue another request to this URL: 'https://secure.bingads.microsoft.com/?reason=1&msg=sessionexpired&rurl=https%3a%2f%2fbingads.microsoft.com%2fReporting%2fv9'
* Adding handle: conn: 0x7fd10e450e00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 2 (0x7fd10e450e00) send_pipe: 1, recv_pipe: 0
* About to connect() to secure.bingads.microsoft.com port 443 (#2)
*   Trying **.**.***.***...
* Connected to secure.bingads.microsoft.com (**.**.***.***) port 443 (#2)
* TLS 1.0 connection using TLS_RSA_WITH_RC4_128_SHA
* Server certificate: bingads.microsoft.com
* Server certificate: VeriSign Class 3 Extended Validation SSL SGC CA
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> POST /?reason=1&msg=sessionexpired&rurl=https%3a%2f%2fbingads.microsoft.com%2fReporting%2fv9 HTTP/1.1
Host: secure.bingads.microsoft.com
Accept: */*
Accept-Encoding: gzip
user-agent: curl/7.30.0 Rcurl/1.95.4.1 httr/0.3
Action: SubmitGenerateReport
Content-Type: text/xml; charset=utf-8
CustomerAccountId: *********
CustomerId: ********
DeveloperToken: ***************
Password: ********
UserName: ***********
Content-Length: 1056
Expect: 100-continue

< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store
< Pragma: no-cache
< Content-Type: text/html; charset=utf-8
< Expires: -1
* Server Microsoft-IIS/8.0 is not blacklisted
< Server: Microsoft-IIS/8.0
* Added cookie JFX_SessionId="********************************" for domain bingads.microsoft.com, path /, expire 0
< Set-Cookie: JFX_SessionId=********************************; domain=bingads.microsoft.com; path=/; secure; HttpOnly
* Added cookie LCIDCookie="1033" for domain bingads.microsoft.com, path /, expire 1430290800
< Set-Cookie: LCIDCookie=1033; domain=bingads.microsoft.com; expires=Wed, 29-Apr-2015 07:00:00 GMT; path=/; secure
* Added cookie MarketIDCookie="1033" for domain bingads.microsoft.com, path /, expire 1430290800
< Set-Cookie: MarketIDCookie=1033; domain=bingads.microsoft.com; expires=Wed, 29-Apr-2015 07:00:00 GMT; path=/; secure
* Added cookie IetfLanguageTag="" for domain bingads.microsoft.com, path /, expire 1430290800
< Set-Cookie: IetfLanguageTag=; domain=bingads.microsoft.com; expires=Wed, 29-Apr-2015 07:00:00 GMT; path=/; secure
< X-AspNetMvc-Version: 3.0
< X-AspNet-Version: 4.0.30319
* Replaced cookie JFX_SessionId="********************************" for domain bingads.microsoft.com, path /, expire 0
< Set-Cookie: JFX_SessionId=********************************; domain=bingads.microsoft.com; path=/; secure; HttpOnly
* Replaced cookie LCIDCookie="1033" for domain bingads.microsoft.com, path /, expire 1430290800
< Set-Cookie: LCIDCookie=1033; domain=bingads.microsoft.com; expires=Wed, 29-Apr-2015 07:00:00 GMT; path=/; secure
* Replaced cookie MarketIDCookie="1033" for domain bingads.microsoft.com, path /, expire 1430290800
< Set-Cookie: MarketIDCookie=1033; domain=bingads.microsoft.com; expires=Wed, 29-Apr-2015 07:00:00 GMT; path=/; secure
* Replaced cookie IetfLanguageTag="" for domain bingads.microsoft.com, path /, expire 1430290800
< Set-Cookie: IetfLanguageTag=; domain=bingads.microsoft.com; expires=Wed, 29-Apr-2015 07:00:00 GMT; path=/; secure
* Added cookie __RequestVerificationToken_Lw__="********************************" for domain secure.bingads.microsoft.com, path /, expire 0
< Set-Cookie: __RequestVerificationToken_Lw__=********************************; path=/; secure; HttpOnly
< X-Powered-By: ASP.NET
< X-UA-Compatible: IE=Edge
< p3p: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
< Date: Tue, 29 Apr 2014 09:45:57 GMT
< Content-Length: 16393
< 
* Connection #2 to host secure.bingads.microsoft.com left intact

I am really looking forward to any help?

Kind regards,


Johannes

Was it helpful?

Solution

You can use the package bingsAdsR from github.

Here is the sample code.

    rm(list=ls())
    # library(devtools)
    # install_github("deepeshgoeliitk/bingAdsR")
    library(bingAdsR)

    ##Variables##
    credentials <- list(
      ###For authorization and access token###
      client_id = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      client_secret = "XXXXXXXXXXXXXXX",
      ###To get data###
      username = "abc@abc.com",
      password = "***********",
      developer_token = "XXXXXXXXXXXXXXXXX",
      customer_id = "XXXXXXXXXX",
      account_id = "XXXXXXX"
    )

    ####AUTHENTICATION#####
    credentials <- baAuthentication(credentials)

    #####Generate the data
    startDate <- "2018-05-17"
    endDate <- "2018-05-17"

    report <- "CampaignPerformanceReport"
    columns <- c("AccountName", "CampaignName", "TimePeriod", "Impressions", "Clicks", "Spend", "Conversions")

    reportId <- getReportId(credentials, report, columns, startDate, endDate)
    downloadUrl <- getDownloadUrl(credentials, reportId)
    df <- getDataFromURL(downloadUrl)

OTHER TIPS

The newest version of the httr lib has build-in oauth2 functions for services like twitter, linkedin, google and azure. The azure oauth2 lib is the one you want if you're trying to connect to the bing ads api v11 with oauth2. The script is here:

https://github.com/hadley/httr/blob/master/demo/oauth2-azure.r

Change this line:

azure_endpoint <- oauth_endpoints('azure')

To this:

azure_endpoint <- oauth_endpoint(authorize = "https://login.live.com/oauth20_authorize.srf", access = "https://login.live.com/oauth20_token.srf")

Now httr is able to connect to the bing api and retrieve the oauth2 token. You will need to modify the script further by entering your application ID, secret and so on.

Note: this does not actually solve the XML issue you've posted here I post this because the v9 and v10 versions of the API will be depreciated later this year and I noticed you're using httr with the v9 bing ads api.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top