gpt4 book ai didi

python - Github api v3 通过 python oauth2 库访问 - 重定向问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:38 24 4
gpt4 key购买 nike

环境 - Python 2.7.3,webpy。

我正在尝试使用 Python web.py 对 github 进行简单的 oauth 3 方式身份验证。根据 github 上的基本 oauth 指南,我正在做这样的事情:

import web,requests
import oauth2,pymongo,json
from oauth2client.client import OAuth2WebServerFlow
urls=('/', 'githublogin',
'/session','session',
'/githubcallback','githubCallback');
class githublogin:
def GET(self):
new_url = 'https://github.com/login/oauth/authorize'
pay_load = {'client_id': '',
'client_secret':'',
'scope':'gist'
}
headers = {'content-type': 'application/json'}
r = requests.get(new_url, params=pay_load, headers=headers)
return r.content

这会将我带到 GH 登录页面。一旦我登录 - GH 不会将我重定向到回调。 redirect_uri参数在github应用中配置。我已经仔细检查以确保这是正确的。

 class githubCallback:
def POST(self):
data = web.data()
print data
def GET(self):
print "callback called"

我在浏览器中看到 http://<hostname>:8080/session和 404 消息,因为我还没有配置 session URL。这是问题 1。问题 2 - 如果我配置 session URL 并打印出发布消息

class session:
def POST(self):
data = web.data()
print data
def GET(self):
print "callback called"

我可以看到一些数据发布到 URL,其中包含名为“authenticity_token”的内容。

我已尝试使用 python_oauth2 库,但无法通过 authorization_url 调用。所以我尝试了这个更简单的请求库。有人可以向我指出这里出了什么问题吗?

最佳答案

这就是我解决这个问题的方法。感谢@Ivanzuzak 的 requestb.in 提示。

我正在使用 Python webpy。

import web,requests
import oauth2,json
urls=('/', 'githublogin',
'/githubcallback','githubCallback');
render = web.template.render('templates/')
class githublogin:
def GET(self):
client_id = ''
url_string = "https://github.com/login/oauth/authorize?client_id=" + client_id
return render.index(url_string)

class githubCallback:
def GET(self):
data = json.loads(json.dumps(web.input()))
print data['code']
headers = {'content-type': 'application/json'}
pay_load = {'client_id': '',
'client_secret':'',
'code' : data['code'] }
r = requests.post('https://github.com/login/oauth/access_token', data=json.dumps(pay_load), headers=headers)
token_temp = r.text.split('&')
token = token_temp[0].split('=')
access_token = token[1]
repo_url = 'https://api.github.com/user?access_token=' + access_token
response = requests.get(repo_url)
final_data = response.content
print final_data

app = web.application(urls,globals())
if __name__ == "__main__":
app.run()

我之前不是用html文件,而是直接从githublogin类发送请求。那没有用。在这里,我使用 html 来引导用户首先从他将登录到 gh 的地方。有了这个,我添加了一个 html 并使用模板渲染它。

def with (parameter)
<html>
<head>
</head>
<body>
<p>Well, hello there!</p>
<p>We're going to now talk to the GitHub API. Ready? <a href=$parameter>Click here</a> to begin!</a></p>
<p>If that link doesn't work, remember to provide your own <a href="http://developer.github.com/v3/oauth/#web-application-flow">Client ID</a>!</p>
</body>
</html>

此文件直接取自开发指南,仅更改了 client_id 参数。

还有一点需要注意的是,在requests.post 方法中- 直接传递pay_load 是行不通的。它必须使用 json.dumps 进行序列化。

关于python - Github api v3 通过 python oauth2 库访问 - 重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com