且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在R的httr包中使用oauth2.0令牌

更新时间:2023-02-16 15:38:10

解决方案



在此特定用例中-与画布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?