通过OData读取数据:如何通过curl/RCurl指定身份验证方案?

2024-02-09

我想通过 HTTPS 读取一些数据OData http://www.odata.org/连接和数据源需要Basic认证方案。我很熟悉平常的curl http://curl.haxx.se//RCurl http://cran.r-project.org/web/packages/RCurl/index.html选项(例如username, userpwd and cainfo),但我不确定如何传递身份验证方案信息。

这是来自为另一个应用程序工作的同事的一些 C# 代码:

var bind = new HttpsTransportBindingElement();
bind.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;

所以我想我需要找出 RCurl 的等效项.NET value System.Net.AuthenticationSchemes.Basic会在 R/curl 世界中吗?


这是我到目前为止想到的:

忽略任何身份验证信息

web_page = getURL(
  url = "https://xxxx.sapbydesign.com/sap/byd/odata/cc_home_analytics.svc/path/to/ressource", 
  username = "<username>",
  userpwd = "*****",
  cainfo = "inst/ssl/ca-bundle.pem"
)

这是我得到的答案(通过 Google Chrome 从德语翻译):

<DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Strict / / EN"
    "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <Meta name = "generator" content =
  "HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />

  <title> Logon Error Message </ title>
  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
  <style type="text/css">
  / * <! [CDATA [* /
      body {font-family: tahoma, helvetica, sans-serif; color: # 333333; background-color: # FFFFFF; } Td {font-family: tahoma, helvetica, sans-serif; font-size: 70%; color: # 333333; {H1} H2 {} } P {font-family: tahoma, helvetica, sans-serif; color: # 333333; margin-top: 4px; margin-bottom: 4px; Ul {} Li} {font-family: tahoma, helvetica, sans-serif; color: # 33333; margin-top: 4px; Emphasize} {color: # 333333; background-color:. # C8E3FF; padding: 5px;} note {color: # CC6600;. } A {font-family: tahoma, helvetica, sans-serif; text-decoration: underline; color: # 336699; } A: visited {color: # 001166; } A: hover {text-decoration: none; }
      / *]]> * /
  </ Style>
</ Head>

<body>
  <table cellpadding="0" cellspacing="0" border="0" width="100%">
    <tr>
      <td>
        <h1> Login Failed </ h1> <br />

        <h2> What happened? </ h2>

        <p> Calling the URL
        https://my310094.sapbydesign.com/sap/byd/odata/cc_home_analytics.svc/RPCRMQUFU_Q0001QueryResults
        was aborted due to incorrect credentials. </ p>
      </ Td>
    </ Tr>

    <tr>
      <td> </ td>
    </ Tr>

    <tr>
      <td class="emphasize">
        <strong> Note </ strong> <br />

        <ul>
          <li> The application was running in the system KHN. </ li>

          <li> The application was for the client 015 and the user and the
          Performed language. </ Li>
        </ Ul>
      </ Td>
    </ Tr>

    <tr>
      <td> </ td>
    </ Tr>

    <tr>
      <td>
        </ p>

        <h2> What can you do? </ h2>

        <ul>
          <li> Check the indication of the clients, users and
          the password for typos. </ li>

          <li> If you do not have a user ID, contact
          your system administrator. </ li>
        </ Ul> <br />

        <p class="note"> error code:
        ICF-LE-https-c:015-l:-u:-a:1-c:015-l:-r:0-T:1-C:1-U:8-P:-L:6-X:00163E0746C81EE484C92B972817350C_00163E0746C81EE484C92B969D80750C_1-x:00163E0746C81EE484C92B972817550C</p><br />


        <p> HTTP 401 - Unauthorized <br /> </ p>

        <p> Your SAP Internet Communication Framework Team </ p>
      </ Td>
    </ Tr>
  </ Table>
</ Body>
</ Html>

指定httpauth

基于这个帖子 https://stackoverflow.com/questions/11173677/how-do-i-disable-the-curlopt-httpauth-option-in-php,我尝试了以下方法:

web_page = getURL(
  url = "https://xxxx.sapbydesign.com/sap/byd/odata/cc_home_analytics.svc/path/to/ressource", 
  username = "<username>",
  userpwd = "*****",
  cainfo = "inst/ssl/ca-bundle.pem",
  httpauth = "CURLAUTH_BASIC"
)

这次,答案看起来有所不同((通过 Google Chrome 翻译自德语):

<DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Strict / / EN"
    "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <Meta name = "generator" content =
  "HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />

  <title> Logon Error Message </ title>
  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
  <style type="text/css">
/ * <! [CDATA [* /
  body {font-family: tahoma, helvetica, sans-serif; color: # 333333; background-color: # FFFFFF; } Td {font-family: tahoma, helvetica, sans-serif; font-size: 70%; color: # 333333; {H1} H2 {} } P {font-family: tahoma, helvetica, sans-serif; color: # 333333; margin-top: 4px; margin-bottom: 4px; Ul {} Li} {font-family: tahoma, helvetica, sans-serif; color: # 33333; margin-top: 4px; Emphasize} {color: # 333333; background-color:. # C8E3FF; padding: 5px;} note {color: # CC6600;. } A {font-family: tahoma, helvetica, sans-serif; text-decoration: underline; color: # 336699; } A: visited {color: # 001166; } A: hover {text-decoration: none; }
  / *]]> * /
  </ Style>
</ Head>

<body>
  <table cellpadding="0" cellspacing="0" border="0" width="100%">
    <tr>
      <td>
        <h1> Login Failed </ h1> <br />

        <h2> What happened? </ h2>

        <p> Calling the URL
        https://my310094.sapbydesign.com/sap/byd/odata/cc_home_analytics.svc was
        aborted due to incorrect credentials. </ p>
      </ Td>
    </ Tr>

    <tr>
      <td> </ td>
    </ Tr>

    <tr>
      <td class="emphasize"> <strong> Note </ strong> <br />
      The application was running in the system KHN. Here were no
      Credentials provided. </ Td>
    </ Tr>

    <tr>
      <td> </ td>
    </ Tr>

    <tr>
      <td>
        </ p>

        <h2> What can you do? </ h2>

        <ul>
          <li> If you do not have a user ID, contact
          your system administrator. </ li>
        </ Ul> <br />

        <p class="note"> error code:
        ICF-LE-https-c:015-l:-u:-a:0-c:015-l:-r:99-T:-C:1-U:-P:-L:6-X:00163E0746C81EE484CA0C65DC12398A_00163E0746C81EE484CA0C65D826798A_1-x:00163E0746C81EE484CA0C65DC12598A</p><br />


        <p> HTTP 401 - Unauthorized <br /> </ p>

        <p> Your SAP Internet Communication Framework Team </ p>
      </ Td>
    </ Tr>
  </ Table>
</ Body>
</ Html>

使用 httr 这真的很容易:

library(httr)

url <- "https://xxxx.sapbydesign.com/sap/byd/odata/cc_home_analytics.svc/path/to/ressource"
r <- GET(url, authenticate("<username>", "*****", "basic"))
stop_for_status(r)
content(r)

(在 httr 0.4 中,您可以删除“基本”,因为基本身份验证现在是默认设置)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过OData读取数据:如何通过curl/RCurl指定身份验证方案? 的相关文章

  • 如何在 R 中抓取受保护的页面(https 链接)(使用 XML 包中的 readHTMLTable)?

    关于如何使用 XML 包中的 readHTMLTable 有很好的答案 我使用常规 http 页面做到了这一点 但是我无法解决 https 页面的问题 我正在尝试阅读该网站上的表格 网址字符串 library RTidyHTML libra
  • 用整数矩阵对 data.frame 进行子集化

    我一直遇到这个问题 想知道是否有一个简单的解决方法 对于某些情况 我发现考虑将矩阵子集化更合乎逻辑 N lt 12 N NA lt 6 dat lt data frame V1 runif N V2 runif N sel mat lt m
  • 带频率图的 R 热图类型图

    I am trying to create a plot like the following 我已经使用 ggplot2 中的 geom tile 粗略地得到了左侧图 但我无法弄清楚如何生成右侧图以及如何将两个图放在一起 Example
  • R - 如何为一个图例元素组合 expression()、paste() 和 formatC() 命令?

    我正在努力创造一个美好的传奇 它应该包含希腊字母 mu 我可以使用表达式 一些文字 即 和 mm 以及使用 formatC 格式化的模型系数来完成此操作 我运行没有问题的是要么没有希腊字母 然后我可以使用简单的粘贴命令 leg txt lt
  • 在绘图中的所有坐标之间绘制线条

    我有以下数据框 data lt data frame x c 5 1 3 2 5 7 12 y c 5 7 6 1 3 5 6 我可以使用 ggplot 函数绘制这些坐标 并在这些坐标之间画一条线 ggplot data aes x y g
  • 使用 R 中绘制的标准误差创建条形图

    我试图找到在 R 中创建条形图并显示标准错误的最佳方法 我看过其他文章 但我无法弄清楚与我自己的数据一起使用的代码 之前没有使用过 ggplot 这似乎是最常用的方法 而 barplot 不与数据框合作 我需要在两种情况下使用它 我为此创建
  • 与 data.table 合并时防止重复列

    我有两个数据表 它们的列名部分相似 dfA lt read table text A B C D E F G iso year matchcode 1 0 1 1 1 0 1 0 NLD 2010 NLD2010 2 1 0 0 0 1 0
  • 在r包中重新导出数据集

    In R包 有可能重新导出函数 这使得很容易回收相同的函数 而不必在不同的包之间重复代码 例如 devtools session info函数是重新导出sessioninfo session info export importFrom s
  • 计算分组序列中两个值之间的差异

    这是这篇文章的后续问题 循环遍历 R 中的数据帧并测量两个值之间的时间差 https stackoverflow com questions 44885856 loop through dataframe in r and measure
  • 尝试通过列表递归时,在 R 中出现错误“递归索引在级别 2 失败”

    当我尝试递归遍历图形顶点列表 将它们的值与列表中的一组颜色进行匹配时 出现错误 递归索引在级别 2 失败 我的颜色列表如下 colrs lt list l blue c red n gray50 然后我有一个 igraph 中的顶点列表vs
  • 在闪亮的应用程序和多个页面中进行身份验证

    在我正在开发的系统中 我有 3 个不同的参与者 用户 管理员 支持团队 使用 Shiny App 我想知道如何向这三个参与者进行身份验证 每个参与者只能访问他们的页面 我发现使用闪亮的服务器专业版可以实现这一点 但它不是免费的 有什么方法可
  • 从 foreach 循环赋值

    我想并行化一个循环 例如 td lt data frame cbind c rep 1 4 2 rep 1 5 rep 1 10 2 names td lt c val id res lt rep NA NROW td for i in l
  • 将非平凡函数应用于 data.table 的有序子集

    Problem 我正在尝试使用我新发现的 data table 功能 永久 来计算一堆数据的频率内容 如下所示 Sample Channel Trial Voltage Class Subject 1 1 1 196 82253 1 1 1
  • R 中有没有快速替换列值的方法?

    假设我们有一个包含数值的数据框 如下所示 Temperature Height 32 157 31 159 33 139 我想更换Height价值观与pic 00001 pic 00002等等 最终结果是 Temperature Heigh
  • 使用facet时ggplot2控制每行的面板数量?

    Is it possible to control the number of panels per row in a ggplot I can only get an equal number of panels on each row
  • 在R中绘制3x3方形网格

    我得到了一个数字列表 n 9 想将它们画在一个 3 3 的正方形网格中 每个网格填充相应的数字 我如何在 R 中执行此操作而不安装额外的软件包 例如情节 非常感谢 这里有一个ggplot解决方案比我预期的要难一点 Setup the dat
  • 如何从类外部更改公共 R6 类方法?

    我希望能够在我的 R6 类中重新定义公共方法 以便它根据该类保存的数据类型进行更改 如下所示 library R6 Simple lt R6Class Simple public list dt mtcars my print functi
  • 无法通过 HTTPS 调用 Web 服务

    我正在开发一个 Net 应用程序 它与 Web 服务通信以获取一些数据 Net 应用程序和 Web 服务之间的连接是通过 HTTPS 完成的 当我从 Net 应用程序调用 Web 服务时 我得到以下堆栈跟踪 System Net WebEx
  • 将函数应用于每个列组合

    我有一个数据框n列并希望对每个列应用一个函数组合列 这与如何cor 函数将数据帧作为输入并生成相关矩阵作为输出 例如 X lt data frame A rnorm 100 B rnorm 100 C rnorm 100 cor X 这将生
  • 在多面图中用 N 注释 x 轴

    我正在尝试生成一些按治疗条件和访问次数细分的数字结果的箱线图 每个框中的观察次数都放在图下方 并且也标记了访问次数 这里有一些虚假数据可以用来说明 我举了两个我尝试过但不太有效的例子 library ggplot2 library plyr

随机推荐