私はgithubにファイルをアップロードできるように、Python3でoauth2クライアントを実装しようとしています。私は非常に基本的なスタートのために、APIを使って認証リストを取得しようとしています。python3 github authorizaitons Oauth2が動作しない
このコードは動作します:
from subprocess import Popen,PIPE
user = 'MYUSERNAME'
pw = 'MYPASSWORD'
git_url = "https://api.github.com/authorizations"
res = Popen(['curl','--user',user + ':' + pw,git_url],stdout=PIPE,stderr=PIPE).communicate()[0]
print(res)
このコードは動作しません。実際には
user = 'MYUSERNAME'
pw = 'MYPASSWORD'
git_url = "https://api.github.com/authorizations"
import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
uri=git_url,
user=user,
passwd=pw)
opener = urllib.request.build_opener(auth_handler)
f = opener.open(git_url)
print(f.read())
を、それがこのエラーを生成します。
Traceback (most recent call last):
File "demo.py", line 18, in <module>
f = opener.open("https://api.github.com/authorizations")
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 375, in open
response = meth(req, response)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 487, in http_response
'http', request, response, code, msg, hdrs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 413, in error
return self._call_chain(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 347, in _call_chain
result = func(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 495, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
私は既存のがあることを知っていますOauth2はPythonで実装されていますが、python3ではなくpython2であり、必要以上に多くのことを行います。
また、Pythonプログラムのcurl
を呼び出すことができます。これは私の欠点です。
私が間違っていることを本当に知りたいです。
ありがとうございました。
、役に立てば幸い?いくつかのOauthレイヤーがすでにリクエストされています(例えば、[here](https://github.com/maraujop/requests-oauth2))が、Python 3でテストされているかどうかはわかりません。 –
Huh。私は要求に精通していません。私は常に依存関係を追加することを躊躇しています。確かめます。ありがとう。あなたはコメントではなく、これを答えにしてみませんか? – vy32
依存関係はすべて悪いことではありません。いつでもそれらを束ねることができます。 (私は提案があったのでコメントしました。解決策ではありません) –