def authorize_user(code, state): url = "https://github.com/login/oauth/access_token" data = "client_id={id}&client_secret={secret}&code={code}&state={state}".format( id = hasbug.conf.github_client_id(), secret = hasbug.conf.github_client_secret(), code = code, state = state) req = urllib2.Request(url, data) req.add_header("Accept", "application/json") res = net.urlopen(req) resdict = json.load(res) token = resdict["access_token"] url = "https://api.github.com/user?access_token={token}".format(token=token) res = net.urlopen(urllib2.Request(url)) return json.load(res)
def shorten_using_googl(url): endpoint = "https://www.googleapis.com/urlshortener/v1/url?key={key}".format(key=conf.google_api_key()) req = urllib2.Request(endpoint, data = json.dumps({ "longUrl": url }), headers = { "Content-Type": "application/json" }) res = net.urlopen(req) obj = json.load(res) return obj["id"]
def add_by_login(cls, bag, login_name): url = User.url_from_login(login_name) opened = net.urlopen(urllib2.Request(url)) toadd = User(json.load(opened)) bag.add(toadd, can_replace=True) return toadd