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

标签 r curl soap bing bing-api

我想直接将数据从 Bing Ads Api 加载到统计工具 R 中。 我用 httr R 中的包,它使用 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

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

亲切的问候,

约翰内斯

最佳答案

您可以使用 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)

关于r - 使用 R 的 Bing Ads API 报告数据 - 获取 ReportRequestId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364799/

相关文章:

r - 根据特定列上的 rep 函数在 R 中的行中查找序列

r - 切断散点图网格线,但不完全在轴限制处标记

c++ - 在 C++ 中使用 cURL 和多线程

r - 如何动态构建字符串并将其传递给 R 中 dplyr 的 mutate() 函数?

php - Curl shell脚本不给php文件变量

laravel - PayPal 沙盒上的 cURL 56 错误

java - SOAP Web 服务上的 XACML 应用程序

php - SOAP 错误 : Parsing WSDL: Couldn't find <definitions> in

java - JAX-WS 密码类型 PasswordText

linux - RODBC 无法识别我的 odbc 设置