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

查看:20
本文介绍了在 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 令牌和使用 httr 发出 GET() 请求的正确方法是什么?

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

推荐答案

解决方案

在这个特定用例中——使用 Canvas API——附加信息在请求的header 中是必需的.

Solution

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

使用 httr R 包中的 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 {secret_key}"))

#进一步说明

  • Explanation of Authorization Header
  • Rationale for why "Bearer " must go before the key.
  • The OAuth Bible is useful for understanding components of a request

其他人能否解释为什么 OP 的示例没有起作用的其他原因?

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

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

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