【问题标题】:Solving 'Cookies must be enabled to use GitHub' using GAE/Webapp2/Urllib2/Python使用 GAE/Webapp2/Urllib2/Python 解决“必须启用 Cookie 才能使用 GitHub”
【发布时间】:2023-12-13 10:01:01
【问题描述】:

尽管浏览了 API 文档,但我找不到任何解释为什么 Github 需要启用 cookie 或如何去做。我可能错过了。

我想在带有 Urllib2 的 Python 中使用 GAE 上的原生 Webapp2 框架,并远离高级库,以便我可以从内到外学习这一点。

我的代码片段:

# 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.

我尝试将以下 (ref) 放在我的文件顶部,但它不起作用:

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

【问题讨论】:

  • 我在尝试为问题添加标签时收到相同的消息。花了2天时间试图弄清楚,感谢任何帮助。注意:我正在尝试在 GitHub Enterprise 中执行此操作(如果有任何不同)。

标签: python google-app-engine python-2.7 urllib2 webapp2


【解决方案1】:

好像和api端点有关。来自官方文档:All API access is over HTTPS, and accessed from the api.github.com domain (or through yourdomain.com/api/v3/ for enterprise). All data is sent and received as JSON.

您收到关于 cookie 的错误,因为您正在调用 GitHub 网站,该网站需要一堆东西才能​​工作,例如 cookie 和 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}"}'

【讨论】:

  • 是的,但这并不能解释为什么它会抛出错误“必须启用 Cookie 才能使用 Github”,或者如何解决这个问题。 IE。您需要在上面编写什么代码才能让 Github 满意,以便它为上述请求(或任何其他请求)返回数据?
  • 您的回答为我指明了正确的方向 - 我的网址错误。应该是https://api.github.com/user。尽管如此,它仍然没有回答我关于如何使用上述技术向 Github API 发出经过身份验证的请求的问题。我正在以更准确的方式重新发布这个问题。谢谢您的帮助!! – Ben Grunfeld 6 分钟前
【解决方案2】:

以下 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 运行

【讨论】: