Reputation: 31
I'm trying to use OAUTH authentication for github in a bash script.
this:
curl -u $USER_NAME --silent -d '{"scopes":["repo"]}' \
https://api.github.com/authorizations
works and as a result I get a response like this:
"created_at": "2012-09-03T13:02:30Z",
"token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"updated_at": "2012-09-03T13:02:30Z",
"note_url": null,
"note": null,
"url": "https://api.github.com/authorizations/620793",
"app": {
"url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api",
"name": "GitHub API"
},
"id": 620793,
"scopes": [
"repo"
]
But I need to keep the "token":
's value in a variable for future use. How could I do that?
Upvotes: 1
Views: 541
Reputation: 171
I did it like this, to get certain data from a different json response:
yourcodehere | grep token | awk '{ print $2 }' | sed s/\"//g | sed s/,//g)
Similar to what Onilton did, but just used awk to get the second item from that line, sed to strip the quotes and comma out. There is at least one json library or something for bash, too, I believe (probably more than one), to parse it for the variable you want, but I haven't even looked at it. I'm not saying this is any better than Onilton's method, but just another option. It works. If I had better awk fu, I could probably do this all with awk, and not need sed, but I don't.
Upvotes: 0
Reputation: 3699
You could use sed and grep to get what you want:
curl -u $USER_NAME --silent -d '{"scopes":["repo"]}' "https://api.github.com/authorizations" | grep '"token":' | sed 's/.*:\s\+"\([^"]\+\).*/\1/g'
I tried to setup the suggest tool, jsawk, to post another alternative here, but jsawk needs spidermonkey-bin, and now you can't get spidermonkey-bin in ubuntu and the PPA that had this packaged don't have it anymore. Now you need to compile from source. Too much trouble, to get a slightly cleaner solution.
Upvotes: 1