librelist archives

« back to archive

flask-oauth and blueprints

flask-oauth and blueprints

From:
Alexis Georges
Date:
2013-03-19 @ 07:12
Hello,

I am trying to setup a new application using flask-oauth for authentication
with Google. I am looking at
http://flask.readthedocs.org/en/latest/blueprints/ and
https://github.com/mitsuhiko/flask-oauth/blob/master/example/google.py for
guidance.

I am having a problem with setting up the google remote_app inside of a
blueprint however, since I don't have access to the app's config in which I
am storing the CLIENT_ID and CLIENT_SECRET.

The only solution I can think of is to have a module (eg. oauth.py) which
includes my app and sets up the remote_app. My blueprint can then import
that and only care about defining routes.

Seems cumbersome. Any suggestions?

Thanks,

Alexis

Re: [flask] flask-oauth and blueprints

From:
Steven Kryskalla
Date:
2013-03-19 @ 07:22
On Tue, Mar 19, 2013 at 12:12 AM, Alexis Georges
<alexis.georges@gmail.com> wrote:
> I am having a problem with setting up the google remote_app inside of a
> blueprint however, since I don't have access to the app's config in which I
> am storing the CLIENT_ID and CLIENT_SECRET.

Try using flask.current_app:

http://flask.pocoo.org/docs/api/#flask.current_app

Re: flask-oauth and blueprints

From:
Alexis Georges
Date:
2013-03-19 @ 07:37
Hi Steven,

I tried that but I received an error saying that I was not running within
an application context (on the go, and don't have the exact error message).
I figure that is because, as in Armin's oauth example, I am creating the
remote_app outside of a request handler.

On Tuesday, March 19, 2013, Steven Kryskalla wrote:

> On Tue, Mar 19, 2013 at 12:12 AM, Alexis Georges
> <alexis.georges@gmail.com <javascript:;>> wrote:
> > I am having a problem with setting up the google remote_app inside of a
> > blueprint however, since I don't have access to the app's config in
> which I
> > am storing the CLIENT_ID and CLIENT_SECRET.
>
> Try using flask.current_app:
>
> http://flask.pocoo.org/docs/api/#flask.current_app
>

Re: [flask] Re: flask-oauth and blueprints

From:
Steven Kryskalla
Date:
2013-03-19 @ 07:56
On Tue, Mar 19, 2013 at 12:37 AM, Alexis Georges
<alexis.georges@gmail.com> wrote:
> Hi Steven,
>
> I tried that but I received an error saying that I was not running within an
> application context (on the go, and don't have the exact error message). I
> figure that is because, as in Armin's oauth example, I am creating the
> remote_app outside of a request handler.

Ok, then I would just define it in a module (e.g. "utils.py") next to
your app, and import the module in both places. That's a normal
technique for sharing code across two modules. Why do you think it's
cumbersome?

If you really want to go through the flask app, you can put any
arbitrary object into app.config (or even on the application object
itself), then access it inside a view method by using current_app.

-Steve

Re: [flask] Re: flask-oauth and blueprints

From:
Harish N
Date:
2013-03-19 @ 09:04
The App must load first, then you have to call current_app. Else you will
get an error message saying that "Application is running outside the
context". Make sure that your app file which contains config details mush
load first.


On Tue, Mar 19, 2013 at 1:26 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Tue, Mar 19, 2013 at 12:37 AM, Alexis Georges
> <alexis.georges@gmail.com> wrote:
> > Hi Steven,
> >
> > I tried that but I received an error saying that I was not running
> within an
> > application context (on the go, and don't have the exact error message).
> I
> > figure that is because, as in Armin's oauth example, I am creating the
> > remote_app outside of a request handler.
>
> Ok, then I would just define it in a module (e.g. "utils.py") next to
> your app, and import the module in both places. That's a normal
> technique for sharing code across two modules. Why do you think it's
> cumbersome?
>
> If you really want to go through the flask app, you can put any
> arbitrary object into app.config (or even on the application object
> itself), then access it inside a view method by using current_app.
>
> -Steve
>



-- 
Thanks & Regards
Harish N
9738930430

Re: [flask] Re: flask-oauth and blueprints

From:
Markus Unterwaditzer
Date:
2013-03-19 @ 09:44
Harish N <harish@regalix-inc.com> wrote:

>The App must load first, then you have to call current_app. Else you
>will
>get an error message saying that "Application is running outside the
>context". Make sure that your app file which contains config details
>mush
>load first.
>
>
>On Tue, Mar 19, 2013 at 1:26 PM, Steven Kryskalla
><skryskalla@gmail.com>wrote:
>
>> On Tue, Mar 19, 2013 at 12:37 AM, Alexis Georges
>> <alexis.georges@gmail.com> wrote:
>> > Hi Steven,
>> >
>> > I tried that but I received an error saying that I was not running
>> within an
>> > application context (on the go, and don't have the exact error
>message).
>> I
>> > figure that is because, as in Armin's oauth example, I am creating
>the
>> > remote_app outside of a request handler.
>>
>> Ok, then I would just define it in a module (e.g. "utils.py") next to
>> your app, and import the module in both places. That's a normal
>> technique for sharing code across two modules. Why do you think it's
>> cumbersome?
>>
>> If you really want to go through the flask app, you can put any
>> arbitrary object into app.config (or even on the application object
>> itself), then access it inside a view method by using current_app.
>>
>> -Steve
>>

This is wrong, application context is not created when instantiating the 
app object. The only "natural" way an app ctx is created is when 
processing a request.

-- Markus (from phone)

Re: [flask] Re: flask-oauth and blueprints

From:
Alexis Georges
Date:
2013-03-19 @ 16:29
Hi Steven,

Not sure why I thought it was cumbersome. It was late. But it does work
that way. :)

Thanks

Alexis


On Tue, Mar 19, 2013 at 12:56 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Tue, Mar 19, 2013 at 12:37 AM, Alexis Georges
> <alexis.georges@gmail.com> wrote:
> > Hi Steven,
> >
> > I tried that but I received an error saying that I was not running
> within an
> > application context (on the go, and don't have the exact error message).
> I
> > figure that is because, as in Armin's oauth example, I am creating the
> > remote_app outside of a request handler.
>
> Ok, then I would just define it in a module (e.g. "utils.py") next to
> your app, and import the module in both places. That's a normal
> technique for sharing code across two modules. Why do you think it's
> cumbersome?
>
> If you really want to go through the flask app, you can put any
> arbitrary object into app.config (or even on the application object
> itself), then access it inside a view method by using current_app.
>
> -Steve
>