使用基本身份验证

时间:2017-06-27 21:48:15

标签: node.js api github

curl - u user_name:password https://api.github.com/repos/:owner/:repo/commits/:ref

这会返回......

  {
 "sha": sha,
 "commit": {
   "author": {
     "name": name,
     "email": "email",
     "date": "2017-06-22T15:03:00Z"
    },
   "committer": {
      "name": "name",
     "email": "email",
     "date": "timestamp"
   },
   "message": "message",
   "tree": {
      "sha": sha,
      "url": url
    },
    "url": url,
    "comment_count": 0
  },
  "url": url,
  "html_url": html,
  "comments_url": comments,
  "author": {
    "login": username,
    "id": id,
    "avatar_url": avatar,
    "gravatar_id": "",
    "url": url,
    "html_url": html,
    "followers_url": followers,
    "following_url": following,
    "gists_url": gists,
    "starred_url": starred,
    "subscriptions_url": subscriptions,
    "organizations_url": organizations,
    "repos_url": repos,
    "events_url": events_url,
    "received_events_url": events,
    "type": "User",
    "site_admin": false
   },
  "committer": {
     "login": login,
     "id": id,
     "avatar_url": avatar,
     "gravatar_id": "",
     "url": url,
     "html_url": html,
     "followers_url": followers,
     "following_url": following,
     "gists_url": gists,
     "starred_url": starred_url,
     "subscriptions_url": subscription,
     "organizations_url": orgs,
     "repos_url": repos,
     "events_url": events_url,
     "received_events_url": events,
     "type": "User",
     "site_admin": false
   },
  "parents": [
     {
      "sha": sha,
      "url": url,
      "html_url": html_url
    }
  ],
  "stats": {
     "total": 2,
     "additions": 1,
     "deletions": 1
   },
   "files": [
     {
       "sha": sha,
       "filename": file_name,
       "status": "modified",
       "additions": 1,
       "deletions": 1,
       "changes": 2,
       "blob_url": blob_url,
       "raw_url": raw_url,
       "contents_url": contents_url,
       "patch": the_data_i_need
     }
  ]
}

上面的内容为我提供了我想要的输出,其中包含payload.files.patch位,我打算解析数据以发送到数据库,该数据库将成为创建我的存储库可视化的源。在postman中运行它也会返回所需的输出。但是,当我运行以下内容时......

var options = {
    url : 'https://api.github.com',
    path : path,
    method : 'GET',
    headers : {'User-Agent':username, 'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64')},
}

 request(options, function(err, res, body){
    if(err){
        console.log(err)
    }
    console.log(body)
})

它返回......

{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}

这似乎只是可能在其文档中列出的通用请求端点。这使我相信我的应用程序需要通过oauth进程注册才能请求此信息。

我目前的设置是我有一个express.js应用程序在aws服务器上监听github webhook。从该有效载荷中,我能够将上述请求拼凑在一起。

但是,我使用基本身份验证从本地计算机上运行了上述内容。我还生成了一个令牌。我没有使用令牌,只是用户名和密码来运行node.js请求代码。

我是否需要更改身份验证?或者curl请求会自动将结果组合在一起,而我需要在node.js代码中隐式执行此操作吗?

2 个答案:

答案 0 :(得分:1)

要在node.js中使用带有request的HTTP基本身份验证,您需要设置一个特定的标头:

var options = {
    headers: {
        'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64'),
        /* some other headers... */
    },
    /* some other options... */           
};

See this question了解更多详情和示例

答案 1 :(得分:1)

您确定path选项吗?

我在request tomcat documentation中看不到任何内容,告诉我这是允许的。

我认为你应该将完整的网址放在url

中 像这样

var options = {
    url : 'https://api.github.com'+path,
    method : 'GET',
    headers : {'User-Agent':username, 'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64')},
}

 request(options, function(err, res, body){
    if(err){
        console.log(err)
    }
    console.log(body)
})