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

This returns...

  {
 "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
     }
  ]
}

The above gets me my desired output containing the payload.files.patch bit which I plan to parse for data to send to a db that will be the source for creating a visualization of my repo. Running this in postman also returns the desired output. However, when I run the following...

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

It returns...

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

Which appears to be just generic request endpoints that likely are listed in their documentation. This leads me to believe that my app needs to be registered via the oauth process in order to request this information.

My current set up is I have an express.js app listening on an aws server for a github webhook. From that payload, i'm able to piece together the above requests.

However, I ran the above from my local machine using basic authentication. I also generated a token. I haven't run the node.js request code using the token, just username and password.

Do I need to change my authentication? Or do curl requests automatically chunk the results together while i would need to implicitly do that within my node.js code?

Yale Newman
  • 1,141
  • 1
  • 13
  • 22

2 Answers2

1

To use HTTP basic auth with request in node.js, you need to set a specific header:

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

See this question for more details and examples

Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
1

Are you sure about the path option?

I don't see anything in the request github documentation telling me that this is allowed.

I think you should put the full url in url,

like this

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)
})
maximede
  • 1,763
  • 14
  • 24