Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving 'Cookies must be enabled to use GitHub' using GAE/Webapp2/Urllib2/Python

Despite looking through the API documentation, I couldn't find anything explaining why Github needs cookies enabled, or how to go about it. I may have missed it tho.

I'd like to use the native Webapp2 framework on GAE in Python with Urllib2, and stay away from high-level libraries so that I can learn this from the inside out.

Snippet from my code:

# 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 returns:

Cookies must be enabled to use GitHub.

I tried putting the following (ref) at the top of my file but it didn't work:

import cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
like image 430
Ben Avatar asked Oct 15 '25 13:10

Ben


1 Answers

It seems to be related to the api endpoint. From the official doc: 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.

You get an error about cookies because you're calling the GitHub website which requires a bunch of stuff to work like cookies and javascript. That's why you need a specific endpoint for the api. The following code sent me back a HTTP 200, note that I'm using the requests library to do HTTP call but you can use whichever you like.

>>> 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}"}'
like image 181
Ketouem Avatar answered Oct 17 '25 07:10

Ketouem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!