使用GAE/Webapp2/Urllib2/Python解决‘必须启用Cookies才能使用GitHub’问题

3 投票
3 回答
17972 浏览
提问于 2025-04-17 21:27

尽管我查阅了API文档,但还是找不到为什么Github需要启用cookies的解释,或者该怎么做。也许我错过了什么。

我想在Python的GAE上使用原生的Webapp2框架和Urllib2,尽量不使用高级库,这样我可以从内部学习。

这是我代码中的一段:

# Get user name
fields = {
    "user" : username,
    "access_token" : access_token
}
url = 'https://github.com/users/'
data = urllib.urlencode(fields)
result = urlfetch.fetch(url=url,
    payload=data,
    method=urlfetch.POST
)

username = result.content

result.content 返回的是:

Cookies must be enabled to use GitHub.

我尝试在文件顶部放入以下内容(参考),但没有成功:

import cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))

3 个回答

-1

我也遇到过同样的问题。我是从我的组织页面开始进入我的代码库的。

-1

下面这个bash函数对我来说很好用:

githubDelRepo(){
    if [[ $# != 2 ]] ; then
        echo "Needs username and repo-name as args 1 and 2 respectively."
    else
        curl -X DELETE -u "${1}" https://api.github.com/repos/"${1}"/"${2}"
    fi
}

把它放到你的~/.bashrc文件里,然后执行source ~/.bashrc,最后用命令githubDelRepo myusername myreponame来运行。

1

这似乎和API的访问地址有关。从官方文档来看:所有的API访问都是通过HTTPS进行的,访问的域名是api.github.com(或者对于企业用户,可以通过yourdomain.com/api/v3/来访问)。所有的数据都是以JSON格式发送和接收的。

你遇到的关于cookies的错误是因为你在调用GitHub网站,而这个网站需要一些东西才能正常工作,比如cookies和JavaScript。这就是为什么你需要一个特定的API访问地址。下面的代码让我得到了一个HTTP 200的响应,注意我使用的是requests库来进行HTTP调用,但你可以使用任何你喜欢的库。

>>> import urllib
>>> import requests

>>> url = "https://api.github.com"
>>> fields = {"user": "Ketouem"}
>>> string_query = urllib.urlencode(fields)
>>> response = requests.get(url + '?' + string_query)
>>> print response.status_code
200
>>> print response.content
'{"current_user_url":"https://api.github.com/user","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos/{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'

撰写回答