github issues api 401,为什么?(django)

21 投票
2 回答
1547 浏览
提问于 2025-04-16 21:32

我正在尝试把GitHub的issues API整合到我的项目中。我觉得我遵循了oauth的规则,并且按照http://develop.github.com/p/issues.html上提到的所有要求来做,但似乎不太奏效。我没有收到详细的错误信息,只得到了一个401的错误。

实际的实现代码使用django和python:

url = 'https://github.com/login/oauth/access_token?client_id=%(client_id)s&redirect_uri=%(redirect_uri)s&client_secret=%(client_secret)s&code=%(code)s' % locals()        
req = urllib2.Request(url)
response = urllib2.urlopen(req).read()
access_token = re.search(r'access_token=(\w+)', response).group(1)
url = 'http://github.com/api/v2/json/issues/open/%(user)s/%(repo)s' % locals()
params = urllib.urlencode({'login': user, 'token': access_token, 'title': 'title', 'body': 'body'})
req = urllib2.Request(url, params)
try:
    response = urllib2.urlopen(req)
except HTTPError, e:
    return HttpResponse('[*] Its a fckin %d' % e.code)
except URLError, e:
    return HttpResponse('[*] %s\n' % repr(e.reason))
else:
    resp = json.loads(response.read())

2 个回答

0

我不确定这是不是你需要的,但这是我在一个项目中用来打开问题的代码:

def issue(self, channel, network, nick, user, title, repoName):
    body = 'Issue sent from %s at %s by %s (registered as %s)' % \
            (channel, network, nick, user.name)
    login = self.registryValue('login')
    token = self.registryValue('token')
    data='title=%s&body=%s&login=%s&token=%s' % (title, body, login, token)
    url = 'http://github.com/api/v2/json/issues/open/' + repoName
    response = json.loads(urllib.urlopen(url, data=data).read())
    id = response['issue']['number']
    return id
2

问题可能是..

params = urllib.urlencode(
    {'login': user, 'token': access_token, 'title': 'title', 'body': 'body'}
)

你指定了标题参数的值是字面上的'title',正文参数也是一样。

你是不是想要这个呢?..

params = urllib.urlencode(
    {'login': user, 'token': access_token, 'title': title, 'body': body}
)

撰写回答