2014-07-24 3 views
0

페이스 북에서 액세스 토큰을 받고 싶습니다. 내가 facepy 라이브러리를 사용하고 있습니다. 아래에 스 니펫을 게시했습니다. python을 사용하여 facebook oauth 토큰을 얻는 방법은 무엇입니까?

# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'face', 
    'facepy', 
) 

FACEBOOK_APPLICATION_INITIAL_PERMISSIONS = ['publish_actions', 'publish_stream', 'manage_pages'] 

는 설정 내 urls.py

urlpatterns = patterns('', 
    url(r'^$', 'face.views.authorize_application', name='authorize_application'), 
    url(r'^rag_the_app/$', 'face.views.get_token', name='get_token'), 
    url(r'^oauth/access_token/$', 'face.views.manage_pages', name='manage_pages'), 
    ) 

이것이 기본 내 views.py

import urllib 
from facepy import GraphAPI 
from facepy.utils import get_extended_access_token 


def authorize_application(request): 

    query = { 
     'client_id': FACEBOOK_APPLICATION_ID, 
     'redirect_uri': 'http://%s:8000/%s' % (FACEBOOK_APPLICATION_DOMAIN, FACEBOOK_APPLICATION_NAMESPACE), 
     'scope': FACEBOOK_APPLICATION_INITIAL_PERMISSIONS 

    } 
    return render(
     request = request, 
     template_name = 'authorization.html', 
     dictionary = { 
      'url': 'https://www.facebook.com/dialog/oauth?%s' % urllib.urlencode(query) 
     }, 
     status = 401 
    ) 



def get_token(request): 

    code = request.GET.get('code', '') 

    query = { 
     'client_id': FACEBOOK_APPLICATION_ID, 
     'redirect_uri': 'http://%s:8000/%s' % (FACEBOOK_APPLICATION_DOMAIN, FACEBOOK_APPLICATION_NAMESPACE), 
     'client_secret': FACEBOOK_APPLICATION_SECRET_KEY, 
     'code': code 
     } 

    return render(
     request = request, 
     template_name = 'authorization.html', 
     dictionary = { 
      'url': 'https://graph.facebook.com/oauth/access_token?%s' % urllib.urlencode(query) 
      }, 
     ) 
    request.session['access_token'] = response['access_token'] 


def manage_pages(request): 

    oauth_access_token = get_extended_access_token(access_token, FACEBOOK_APPLICATION_ID, FACEBOOK_APPLICATION_SECRET_KEY) 
    graph = GrahAPI(oauth_access_token) 
    page_id = '263880043816054' 
    og_path = "%d/feed" %page_id 
    graph.post(path = og_path, message = "hello page") 

페이스 북 앱입니다

canvas url: http://localhost:8000 
secure canvas url: https://localhost:8000 
app_domain: localhost:8000 

페이스 북 앱 고급 설정

유효한 OAuth를 내가 내 서버를 실행하면

http://localhost:8000/oauth/access_token/ 

, 그것은

Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains. 

가 어떻게 액세스를 얻을 수있는 오류를 발생시킵니다 URI를 리디렉션 토큰? 무엇이 잘못 되었나요?

답변

0

dev 서버를 인터넷에 노출시켜야 페이스 북이 콜백을 할 수있는 방법을 제공해야합니다. localhost는 사용할 수 없습니다.

의 모든 :

  • 캔버스 URL
  • 안전한 캔버스 URL
  • 유효한 OAuth를 리디렉션 URI를

...에 도달 가능한 서버의 도메인/URL이어야한다 Internets.

+0

"로컬 호스트"가 "이 컴퓨터"를 의미하기 때문에 리디렉션 uri에 내 로컬 호스트를 사용할 수없는 이유는 –

+0

입니다. 따라서이 값을 "localhost"로 설정하면 oauth 플로우를 처리 할 페이스 북 서버는 로컬 호스트로 리다이렉션을 시도하므로 (토폴로지 관점에서 보면) 토큰이 전달 된 콜백을받지 못합니다 귀하의 서버. (당신의 서버 : 사용자가 페이스 북으로 로그인 클릭) -> [페이스 북 : 사용자가 승인했는지] -> [당신의 서버 : 승인/거부가있는 페이스 북에서의 콜백]'. 페이스 북은 토큰 (또는 거절)으로 콜백을 제공하기 위해 귀하에게 연락 할 수 있어야합니다. –

+0

그래서 내 앱을 로컬에서 테스트하고 싶다면 어떻게 할 수 있습니까? –

관련 문제