请求Github API使用R创建一个存储库 [英] Request Github API to create a repository with R
问题描述
我编写了一些代码来在Github上使用Git使用Github API创建一个存储库。我的认证工作正常,我可以做一些事情,比如请求存储库和类似的东西......但是当创建一个仓库,我总是得到状态 400
,并作为消息解析JSON
的问题。
这是我用于验证的代码(您需要更改您的应用程序和密码): 在这里我请求创建一个新的repo,基于这里的文档:
<$ p $ b $ library(httr)
library(RCurl)
#1.查找github的OAuth设置:
#http://developer.github .com / v3 / oauth /
oauth_endpoints(github)
#2.在https://github.com/settings/applications注册一个应用程序
#将您的下面的值 - 如果秘密被省略,它会在
#中查找它的GITHUB_CONSUMER_SECRET环境变量。
#
#使用http:// localhost:1410作为回调url $ b $ myapp< - oauth_app(TestApp,app-number,secret-number)
$ lt; - 'public_repo'
#3.获取OAuth凭证
github_token< - oauth2.0_token(oauth_endpoints(github),scope = scope,myapp)
https://developer.github.com/v3/repos/#create
#存储库到Github
url =https://api.github.com/user/repos
data = list (name=newRepo)
r <-POST(url,body = data,config(token = github_token))
content(r)
任何有此经验的人都可以提供帮助吗?
谢谢!
请注意 encode =json,它会执行此操作。
r < - POST(url,body = data,encode =json,config(token = github_token))
I wrote some code to create a repository on Github using the Github API using R. I have the authentication working properly, and I can do things like request the repositories and things like this....but when it comes to create a repository, I get always status 400
and as a message Problems parsing JSON
.
This is the code I use to authenticate (you need to change your app and secret numbers):
library(httr)
library(RCurl)
# 1. Find OAuth settings for github:
# http://developer.github.com/v3/oauth/
oauth_endpoints("github")
# 2. Register an application at https://github.com/settings/applications
# Insert your values below - if secret is omitted, it will look it up in
# the GITHUB_CONSUMER_SECRET environmental variable.
#
# Use http://localhost:1410 as the callback url
myapp <- oauth_app("TestApp", "app-number","secret-number")
scope <- 'public_repo'
# 3. Get OAuth credentials
github_token <- oauth2.0_token(oauth_endpoints("github"),scope=scope, myapp)
And here I do the request to create a new repo, based on the documentation here: https://developer.github.com/v3/repos/#create
#Push repository to Github
url = "https://api.github.com/user/repos"
data = list("name"= "newRepo")
r<-POST(url,body = data,config(token = github_token))
content(r)
Anyone with some experience with this can help?
Thanks!
Note the encode = "json", it will do the trick.
r <- POST(url,body = data, encode = "json", config(token = github_token))
这篇关于请求Github API使用R创建一个存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!