2016-07-28 11 views
2

私はPythonでGitHub資格情報を確認しようとしています。GitHub資格情報の有効性を確認してください

import urllib2, base64 

username = "[email protected]" 
password = "password" 

request = urllib2.Request("https://github.com/") 
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') 
request.add_header("Authorization", "Basic %s" % base64string) 
result = urllib2.urlopen(request) 

if result.code == 200: 
    print "Success" 
else: 
    print "Error" 

をしかし、それは常にさえ間違ったパスワードを使用して、Success返します

私はこれを試してみました。私は間違って何をしていますか?

答えて

2

変更:

それにもかかわらず、私はこのコードが簡潔になりrequestsサードパーティのライブラリを使用します。それは私のために働いています。

~/ $ python2 --version 
Python 2.7.6 
~/ $ uname -a 
Linux wlysenko-Aspire 3.13.0-37-generiC#64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 
でテスト

1

https://github.com/の代わりにhttps://api.github.com/userを使用していたはずです。あなたの資格情報を変更してみ

request = urllib2.Request("https://api.github.com/") 

にライン

request = urllib2.Request("https://github.com/") 

import requests 
print requests.get(
    'https://api.github.com/user', 
    auth=('username', 'password') 
) 
関連する問題