对于家庭作业(在Signavio工作流加速器中),我需要使用GitHub-API v3将用户添加到Github上的组织。代码必须用JavaScript编写,这是一种我不是很熟悉的语言。
目前,我得到了以下错误代码:"SyntaxError: Request._callback的位置1处的JSON中的意外令牌o“。所以我有一种感觉,可能是解析出了问题。
var link = 'https://api.github.com/orgs/myorganization/memberships/' + githubUser
var token = 'mytoken'
request({url: link, method: 'put', headers: {'User-Agent': 'request'}, auth: {username: token}, JSON: true},
function (response, body) {
console.log(body)
if(body !== undefined){
body = JSON.parse(body)
body['state'][0]['main']
status = body['main']['state']
status = body.main.state
}
else{
status = 'error'
}
})我不知道这是否会有帮助,但如果我使用cURL执行这个put请求,它会工作,答案是这样开始的:
{
"url": "https://api.github.com/orgs/myorganization/memberships/githubUser",
"state": "pending",
...}所以这个"state“就是我想在上面的代码中读取的值。
已经感谢你的帮助了!
发布于 2018-12-29 00:02:17
我和我的一个朋友一起工作,我们一起找到了一个可行的解决方案。所以,如果其他人也有同样的问题:这段代码就是魔术!
var link = 'https://api.github.com/orgs/myorganization/memberships/' + githubUser
var token = 'mytoken'
const options = {
url: link,
method: 'put',
headers: {'User-Agent': 'request'}, auth: {username: token}
}
function callback(error, response, body) {
console.log(error)
if(!error && response.statusCode == 200){
const info = JSON.parse(body)
status = info['state'][0]['main']
console.log(status)
status = info['state']
status = info.state
}
console.log(body)
}
request(options, callback)https://stackoverflow.com/questions/53959333
复制相似问题