R 中使用的 Github API 的授权码 [英] authorization code for Github API used in R

查看:89
本文介绍了R 中使用的 Github API 的授权码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问 API 以获取有关 http://github.com 的信息.我在 github 中的应用程序(在开发人员应用程序中)为此 URL 创建了应用程序,并尝试使用 httr 库通过 R 访问.以下是代码

I am trying Access the API to get information on http://github.com. I created in application in github (in developer application) for this URL and try to access thru R using httr libraries. The following is the code

library(httr)
oauth_endpoints("github")
myapp <- oauth_app("github",key = "#####################",secret = "########################" )

(key 替换为 client id,secret 替换为 secred id)

(key was replaced with client id and secret was replaced with secred id)

github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)

这提示了我以下

使用本地文件在 R 会话之间缓存 OAuth 访问凭据?1:是的2:没有

Use a local file to cache OAuth access credentials between R sessions? 1: Yes 2: No

我选择了 2(因为我之前尝试过选项 1)然后显示以下内容

I selected 2 (as i tried option 1 earlier) then the following are displayed

httpuv not installed, defaulting to out-of-band authentication
Please point your browser to the following url: 

  https://github.com/login/oauth/authorize?client_id=72939e1b6d499f4f1894&scope=&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code

输入授权码谁能告诉我授权码是什么?

Enter authorization code Can any one tell me what the authorization code is?

推荐答案

授权码是 github 在正确的 OAuth 2.0 'dance'(使用 Hadley Wickham 的术语)后提供的代码.最简单的方法是使用 httpuv (install.packages("httpuv")).安装后,本地网络服务器将在端口 1410 上设置并提供您已正确设置您的 github 应用程序(重定向到 http://localhost:1410)a>).

The authorisation code is the code that github supplies after a correct OAuth 2.0 'dance' (to use Hadley Wickham's term). The easiest way of doing this is to use httpuv (install.packages("httpuv")). With that installed, a local webserver is setup on port 1410 and provided you've setup your github application appropriately (with a redirect to http://localhost:1410).

如果您没有安装 httpuv,那么 httr 的 OAuth 2.0 功能默认为带外授权.这要求 GitHub 重定向到 urn:ietf:wg:oauth:2.0:oob&response_type=cod,它应该在浏览器中显示授权码,以便可以复制和粘贴.但是,几乎可以肯定,您的重定向 URL 设置了不同的设置,因此 github 抱怨重定向 URI 不匹配.我不确定是否可以将 github 配置为允许 oob 重定向(但我刚刚尝试过,但似乎没有).

If you don't have httpuv installed, then httr's OAuth 2.0 function defaults to out of band authorisation. This asks GitHub to redirect to urn:ietf:wg:oauth:2.0:oob&response_type=cod which should display the authorisation code within the browser so that it can be copied and pasted. However, you've almost certainly got something different set as your redirect URL and so github complains that there is a redirect URI mismatch. I'm not sure whether github can be configured to allow the oob redirect (but I've just tried and it doesn't seem to).

不使用 httpuv 的唯一原因是,如果您在不允许您在端口 1410 上设置服务器的机器上使用 R,或者您通过 RStudio 服务器或 SSH 会话在远程机器上使用 R.在后一种情况下,网络服务器将设置在远程机器上,但您的浏览器将尝试连接到本地机器上的端口 1410.您可以通过将 SSH 端口从本地机器上的端口 1410 转发到远程机器上的端口 1410 来解决这个问题.

The only reasons not to use httpuv are if you are using R on a machine that won't let you set up a server on port 1410 or if you are using R on a remote machine via RStudio Server or an SSH session. In the latter case, the webserver will be setup on the remote machine, but your browser will be trying to connect to port 1410 on your local machine. You could potentially get around this by doing SSH port forwarding from port 1410 on your local machine to port 1410 on the remote machine.

还要注意 https://github.com/hadley/上的演示代码httr/blob/master/demo/oauth2-github.r 与当前 CRAN 版本的 oauth2-github 演示不同,oauth2-github 演示包含 Hadley 应用程序的秘密,因此您可以按原样运行演示,而无需先设置您自己的应用程序.

Note also that the demo code at https://github.com/hadley/httr/blob/master/demo/oauth2-github.r unlike the current CRAN version of the oauth2-github demo includes the secret for Hadley's application so you can run the demo as is without setting up your own application first.

这篇关于R 中使用的 Github API 的授权码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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