Skip to content

Commit 0a64003

Browse files
committed
almost there, suddenly getting an authentication error for no good reason
1 parent 827a10c commit 0a64003

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

OAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var request = require("request")
22
, qs = require("qs")
33
, credential = require("./credential")
4-
, USER_AGENT = require("user_agent")
4+
, USER_AGENT = require("./user_agent")
55

66

77
// getting authorization
@@ -27,7 +27,7 @@ function get_token(ready){
2727
})
2828
, 'auth': credentials
2929
}
30-
30+
console.log(r)
3131
request.post(r, ready)
3232
})
3333
}

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
#!/usr/bin/env node
22

3-
var anonymous = require("./post")
3+
var post_gist = require("./post")
44
, program = require("commander")
55

66
program
77
.version("0.0.0")
88
.usage("[options] <file1, file2>")
9-
.option("-p, --private", "Make a private gist by reading your authentication details from `git credential fill` (public by default).")
9+
.option("-u, --public", "Make a public gist on your account (It's private by default). Needs to read your authentication details from `git credential fill`.")
1010
.option("-a, --anonymous", "Make an anonymous gist")
1111
.option('-d, --description <string>', "A description for your gist", String)
1212

1313
program.on("--help", function(){
1414
//additional help?
1515
})
1616

17+
// TODO, if (program.args.length === 0){
18+
// // Open editor and then on quite make a
19+
// // gist of the file
20+
// }
21+
1722
program.parse(process.argv)
1823

19-
var privacy = ["anonymous", "private"]
20-
.filter(function(d){return program[d]})
24+
var privacy = ["anonymous", "public", "private"]
25+
.some(function(d, i, arr){
26+
privacy = arr[i]
27+
if (arr[i] == "private"){
28+
return true
29+
}
30+
return program[d]
31+
})
2132

22-
anonymous(program.args, process.description)
33+
post_gist(program.args, privacy, process.description)

post.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
module.exports = simple_post
22

3-
credential = require("./credential")
4-
request = require("request")
5-
path = require("path")
6-
fs = require("fs")
3+
var USER_AGENT = require("./user_agent")
4+
, oauth = require("./OAuth")
5+
, request = require("request")
6+
, path = require("path")
7+
, fs = require("fs")
78

89

10+
// file_names is a list of file names, privacy is an array with three possible
911
function simple_post(file_names, privacy, description){
1012
// iterate through each file, opening it and reading the contents
1113

1214
var payload
1315
var counter = 0
1416
, files_left = file_names.length
1517
, files = {}
18+
, privacy_flag
19+
1620

1721
payload = {
1822
description: description
1923
, public: true
2024
, files: files
2125
}
2226

27+
if (privacy === "public" || privacy === "private") {
28+
// otherwise this needs to be not set
29+
payload.public = privacy === "public"
30+
}
2331

2432
for (var i = 0, len = file_names.length; i < len; ++i){
2533
fs.readFile(file_names[i], handler)
26-
2734
function handler(err, data){
2835
// got to save a local reference to i, cause by the time these get called
2936
// the enclosing scope will have changed
@@ -41,9 +48,24 @@ function simple_post(file_names, privacy, description){
4148
function done(payload){
4249
var r = {
4350
url: "https://api.github.com/gists"
44-
, headers: {"User-Agent": "nodejs/0.0.1 (node) gist command line tool v0.0.1 by @AWinterman" }
51+
, headers: {"User-Agent": USER_AGENT }
4552
, json: payload
4653
}
47-
request.post(r, function(err, data){ console.log("url: ", data.body.html_url)})
54+
55+
if (payload.public !== undefined){
56+
// then we need to get an Oauth token
57+
oauth(function(err, data){
58+
r.headers.Authorization = "bearer " + data.body.token
59+
request.post(r, report)
60+
})
61+
} else {
62+
request.post(r, report)
63+
}
4864
}
4965
}
66+
67+
function report(err, data){
68+
// TODO open browser automatically
69+
console.log(data.body)
70+
console.log("url: ", data.body.html_url)
71+
}

0 commit comments

Comments
 (0)