尽管查看了 API 文档,但我找不到任何解释为什么 Github 需要启用 cookie 或如何实现的内容。我可能错过了。
我想在 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.
我尝试将以下内容(ref)放在我的文件顶部,但它不起作用:
import cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
好像和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}"}'
<iframe width="806" height="453" src="https://www.youtube.com/embed/6u7h7-RP_p8" title="A more private ecosystem on Chrome and Android" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> `enter code here`
以下 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 运行
我也有同样的问题。我从我的组织页面开始进入我的存储库。