2013-03-05 74 views
2

我想通過Python從Foursquare API獲取訪問令牌。Foursquare API Python訪問令牌錯誤

我使用的包裝從https://github.com/mLewisLogic/foursquare 而且,隨後於http://log.hckr.org/2012/02/01/foursquare-api-wrapper-for-python

提到的相同方法,但,對於access_token = client.oauth.get_token('XX_CODE_RETURNED_IN_REDIRECT_XX')。我收到以下錯誤

Traceback (most recent call last): 
    File "<interactive input>", line 1, in <module> 
    File "C:\Python27\lib\site-packages\foursquare\__init__.py", line 135, in get_token 
    response = _request_with_retry(url) 
    File "C:\Python27\lib\site-packages\foursquare\__init__.py", line 707, in _request_with_retry 
    return _process_request_with_httplib2(url, data) 
    File "C:\Python27\lib\site-packages\foursquare\__init__.py", line 733, in _process_request_with_httplib2 
    raise FoursquareException(u'Error connecting with foursquare API') 
FoursquareException: Error connecting with foursquare API 

我不知道如果錯誤是因爲使用httplib2庫或其他什麼東西的!

任何人有任何解決方案?

+0

能在你身邊張貼更多的代碼這個調用嗎?可以肯定的是,你實際上傳入了代碼(可以作爲查詢字符串參數),而不是字符串「XX_CO DE_RETURNED_IN_REDIRECT_XX'',對嗎? – octopi 2013-03-05 23:28:16

+0

@octopi我已經嘗試了http://log.hckr.org/2012/02/01/foursquare-api-wrapper-for-python – Zero 2013-03-12 18:04:51

+0

上提到的代碼實際記錄的錯誤是什麼? – 2013-06-26 18:03:45

回答

1

它看起來像你選擇的包裝可能會過時。

如果您打算使用更通用的OAuth消費者解決方案,我可以推薦rauth。 Rauth是圍繞請求構建的簡單的OAuth 1.0/a和2.0客戶端。基本上你想要做的是將FourSquare設置爲一項服務。我們可以修改Facebook的例子中,我們提供了rauth像這樣:

from rauth import OAuth2Service 

import re 
import webbrowser 

# Get a real client_id and client_secret from: 
# https://developer.foursquare.com/overview/auth#registration 

foursquare = OAuth2Service(
    client_id=xxx, 
    client_secret=xxx, 
    name='foursquare', 
    authorize_url='https://foursquare.com/oauth2/authenticate', 
    access_token_url='https://foursquare.com/oauth2/access_token', 
    base_url='https://api.foursquare.com/v2/') 

# This should redirect to your app, may function as a demo 
# without updating, but be sure to update once you're done 
# experimenting! 
redirect_uri = 'https://example.com/' 

params = {'response_type': 'token', 
      'redirect_uri': redirect_uri} 

authorize_url = foursquare.get_authorize_url(**params) 

print 'Visit this URL in your browser: ' + authorize_url 
webbrowser.open(authorize_url); 

url_with_code = raw_input('Copy URL from your browser\'s address bar: ') 
access_token = re.search('\#access_token=([^&]*)', url_with_code).group(1) 
session = foursquare.get_session(access_token) 

user = session.get('users/self').json()['response']['user'] 

print 'currently logged in as: ' + user['firstName'] 

我還沒有運行該代碼,但隨時給它一個去(記住,你得把服務構造自己的憑證,即client_idclient_secret參數之前,它會正常運行。祝你好運,讓我知道如何去,如果你決定給它一個鏡頭!

充分披露,我保持rauth。

+0

截至發佈此評論時,封裝仍然受支持並通過所有實時單元測試。 – 2013-06-26 17:58:28