在R的httr包中使用oauth2.0令牌 [英] Using oauth2.0 tokens with R's httr package

查看:116
本文介绍了在R的httr包中使用oauth2.0令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

软件包 httr 在R中提供了CURL包装器(请参见软件包文档)。

The package httr provides CURL wrappers in R (see package documentation).

我是HTTP和API的新手。我的麻烦是让oauth2.0身份验证起作用。我尝试了各种语法,并得到错误或状态401。

I'm brand new to HTTP and APIs. My trouble is getting oauth2.0 authentication to work. I have tried various syntax and get either errors or status 401.

使用oauth2.0令牌并生成 GET( )使用 httr 的请求?

What is the correct way to use an oauth2.0 token and make a GET() request using httr?

# Set UP
  
  url = "https://canvas.{institution}.edu/api/v1/courses"
  key = "{secret_key}"

# 1
  GET(url, sign_oauth2.0(key)) 
  # Error: Deprecated: supply token object to config directly

# 2
  GET(url, config(sign_oauth2.0 = key)) 
  # unknown option: sign_oauth2.0

# 3
  GET(url, config = list(sign_oauth2.0 = key)) 
  # Status 401


推荐答案

解决方案



在此特定用例中-与画布API -请求的标头中需要其他信息。

Solution

In this particular use case—working with the Canvas API—additional information is required in the header of the request.

使用< httr R包中的code> GET 函数,使用 add_header 参数提供参数,包括您的oauth2键。

Using the GET function from the httr R package, use the add_header parameter to supply the argument including your oauth2 key.

如果您不想将密钥硬编码到请求中,请使用选项1(推荐)。或者,使用选项2并将密钥作为字符串插入。但是在两种情况下,键都是 Bearer。

Use Option 1 if you don't want to hard code your key into the request (recommended). Or, use Option 2 and insert the key as a string. But in both cases, "Bearer " precedes the key.

# Set Up
url = "https://canvas.{institution}.edu/api/v1/courses"
key = "{secret_key}"

# OPTION 1
GET(url, add_headers(Authorization = paste("Bearer", key, sep = " ")))

# OPTION 2
courses.request = GET(url, add_headers(Authorization = "Bearer {secretkey}"))



进一步的解释




  • 授权标头
  • 的说明
  • Rationale为什么持票人 必须在密钥之前。

  • OAuth圣经对于理解请求的组成部分很有用

  • Further Explanations

    • Explanation of Authorization Header
    • Rationale for why "Bearer " must go before the key.
    • The OAuth Bible is useful for understanding components of a request
    • 其他任何人都可以解释其他原因吗? ns为什么OP的示例没有起作用?

      Can anyone else explain other reasons why the OP's examples didn't work?

      这篇关于在R的httr包中使用oauth2.0令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆