3

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))
Community
  • 1
  • 1
Ben
  • 5,085
  • 9
  • 39
  • 58
  • I am getting the same message when I try to add labels to an issue. Have spent 2 days trying to figure it out, any help is appreciated. Note : I am trying to do it in GitHub Enterprise ( if it makes any difference ). – PresidentPratik Oct 01 '18 at 18:31

3 Answers3

1

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}"}'
Ketouem
  • 3,820
  • 1
  • 19
  • 29
  • Yes, but that doesn't explain why it throws the error 'Cookies must ben enabled to use Github', or how to solve it for that matter. I.e. what code would you need to write above to make Github happy so that it returns data for the above request (or any other for that matter)? – Ben Mar 08 '14 at 21:37
  • Your answer pointed me in the right direction - my url was wrong. It should have been `https://api.github.com/user`. Despite that, it still doesn't answer my question of how to use the above tech to make an authenticated request to the Github API. I'm reposting this question in a more accurate fashion. Thank you for your help!! – Ben Grunfeld 6 mins ago – Ben Mar 08 '14 at 23:07
0

The following bash function worked fine for me:

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
}

put in ~/.bashrc then source ~/.bashrc and run with githubDelRepo myusername myreponame

methuselah-0
  • 96
  • 1
  • 5
-1

I had the same problem. I got into my repos by starting from my organization page.

carolceb
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 05 '22 at 21:37
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33091575) – hide1nbush Nov 07 '22 at 08:24