实瓶帆布的应用程序 - 405方法不被允许 [英] Flask facebook canvas app - 405 method not allowed

查看:218
本文介绍了实瓶帆布的应用程序 - 405方法不被允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的Web开发和Python,试图让使用python瓶一个Facebook应用程序。发现在本教程中,我使用一些开始code:的http:// ryaneshea.com/facebook-authentication-for-flask-apps

在Facebook的OAuth认证工作,在第一时间用户使用他们被要求给应用其权限的应用程序。随后,他们都应该被重定向到应用程序索引网站。如果应用程序正在从Facebook中使用它,然后给出了消息:405法不允许的。如果应用程序正在从Facebook之外使用(从我的Apache Web服务器)重定向的工作原理。

当应用程序被从Facebook的只有GET请求是由外使用,但来自Facebook的内部使用时,这是POST请求,让405错误:

 POST / fb_source =搜索和; REF = br_tf HTTP / 1.1,

如何接受来自Facebook的POST请求,因此用户可以重定向?任何建议

下面是code的相关部分:

 从瓶进口render_template,url_for,请求,会话重定向从flask_oauth进口的OAuthOAuth的OAuth的=()Facebook的= oauth.remote_app('Facebook的,                        BASE_URL =的https://graph.facebook.com/',
                        request_token_url =无,
                        access_token_url ='/ OAuth的/的access_token',
                        authorize_url =的https://www.facebook.com/dia​​log/oauth',
                        CONSUMER_KEY = FACEBOOK_APP_ID,
                        CONSUMER_SECRET = FACEBOOK_APP_SECRET,
                        request_token_params = {'范围':'电子邮件,'}
)应用=瓶(__ name__)@ facebook.tokengetter
高清get_facebook_token():
    返回session.get('facebook_token')高清pop_login_session():
    session.pop('LOGGED_IN',无)
    session.pop('facebook_token',无)@ app.route(/)
高清指数():
    返回render_template('的index.html')@ app.route(/ facebook_login)
高清facebook_login():
    返回facebook.authorize(回调= url_for('facebook_authorized',
                                               接下来= request.args.get(下一步),_external = TRUE))@ app.route(/ facebook_authorized)
@ facebook.authorized_handler
高清facebook_authorized(RESP):
    next_url = request.args.get(下一步)或url_for('指数')
    如果RESP是无或ACCESS_TOKEN不RESP:
        返回重定向(next_url)    会议['LOGGED_IN'] = TRUE
    会话['facebook_token'] =(相应['ACCESS_TOKEN'],'')    返回重定向(next_url)


解决方案

我通过指定GET和POST方法相应的路由固定它。不得不重启apache得到它的工作。

这是语法:

  @ app.route(/ facebook_authorized,方法= ['GET','POST'])

I am fairly new to web development and Python, trying to make a facebook app using python flask. Found some code in this tutorial that I am using to get started: http://ryaneshea.com/facebook-authentication-for-flask-apps

The Facebook OAuth authentication is working, the first time a user uses the app they are asked to give the app their permissions. Afterwards they are supposed to be redirected to the applications index site. If the app is being used from within Facebook it then gives the message: "405 method not allowed". If the app is being used from outside of Facebook (from my apache web server) the redirection works.

When the app is used from outside of Facebook only GET requests are made, but when used from inside of Facebook this is the POST request that gives the 405 error:

"POST /?fb_source=search&ref=br_tf HTTP/1.1""

Any suggestions on how to accept the POST requests from Facebook so the user can be redirected?

Here are the relevant parts of the code:

from flask import render_template, url_for, request, session, redirect

from flask_oauth import OAuth

oauth = OAuth()

facebook = oauth.remote_app('facebook',

                        base_url='https://graph.facebook.com/',
                        request_token_url=None,
                        access_token_url='/oauth/access_token',
                        authorize_url='https://www.facebook.com/dialog/oauth',
                        consumer_key=FACEBOOK_APP_ID,
                        consumer_secret=FACEBOOK_APP_SECRET,
                        request_token_params={'scope': 'email, '}
)

app = Flask(__name__)

@facebook.tokengetter
def get_facebook_token():
    return session.get('facebook_token')

def pop_login_session():
    session.pop('logged_in', None)
    session.pop('facebook_token', None)

@app.route("/")
def index():
    return render_template('index.html')

@app.route("/facebook_login")
def facebook_login():
    return facebook.authorize(callback=url_for('facebook_authorized',
                                               next=request.args.get('next'), _external=True))

@app.route("/facebook_authorized")
@facebook.authorized_handler
def facebook_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None or 'access_token' not in resp:
        return redirect(next_url)

    session['logged_in'] = True
    session['facebook_token'] = (resp['access_token'], '')

    return redirect(next_url)

解决方案

I fixed it by specifying both GET and POST methods for the appropriate routes. Had to restart apache to get it working.

This is the syntax:

@app.route("/facebook_authorized", methods=['GET', 'POST'])

这篇关于实瓶帆布的应用程序 - 405方法不被允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆