使用R的Bing Ads API报告数据 - 获取ReportRequestId

时间:2014-04-29 12:33:57

标签: r curl soap bing bing-api

我想直接将 Bing Ads Api 中的数据加载到统计工具 R 中。 我使用R中的httr包,它使用 Curl 来启用R和API之间的连接。 根据API manual,我尝试 SubmitGenerateReport ,但我没有收到 ReportRequestId

这是R代码:

#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)

Api返回以下内容:

* 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

我真的很期待任何帮助吗?

亲切的问候,

约翰

2 个答案:

答案 0 :(得分:1)

最新版本的httr lib具有内置的oauth2功能,可用于twitter,linkedin,google和azure等服务。如果您尝试使用oauth2连接到bing ads api v11,则可以使用azure oauth2 lib。脚本在这里:

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

更改此行:

azure_endpoint <- oauth_endpoints('azure')

对此:

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

现在httr能够连接到bing api并检索oauth2令牌。您需要通过输入应用程序ID,密码等来进一步修改脚本。

注意:这实际上并没有解决您在此处发布的XML问题我发布此问题是因为API的v9和v10版本将在今年晚些时候折旧,我注意到您正在使用httr与v9 bing ads api。

答案 1 :(得分:1)

您可以使用github包中的bingsAdsR。

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)