Using oauth2.0 tokens with R's httr package

后端 未结 1 1138
旧巷少年郎
旧巷少年郎 2021-01-05 02:05

Problem

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

I\'m brand new to HTTP and APIs. My trouble is getting oauth2.

1条回答
  •  天涯浪人
    2021-01-05 02:40

    Solution

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

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

    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}"))
    

    #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

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

    0 讨论(0)
提交回复
热议问题