File: verifier_callback.rst

package info (click to toggle)
python-pypump 0.3%2Bgit20130823.1.97bffc6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 328 kB
  • ctags: 333
  • sloc: python: 1,630; makefile: 140; sh: 7
file content (90 lines) | stat: -rw-r--r-- 2,891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
================
Getting Verifier
================

The default behavour of PyPump is to ask the user (in english) to click on a link
and paste in the verifier. It's done via a print statement and raw_input which usually
occur over standard in and standard out. Often this isn't very useful so PyPump provides
a very easy way using two callbacks to work with your program to ask them for the verifier.

We have a link the user must click, it'll take them to an Authorization page asking
them to permit your program access, if they allow your program they will be taken to a
page that will have the verifier on, this needs to be entered into your program.

The default experiance via PyPump
----------------------------------
This is how PyPump by default asks::

    To allow us to use your pump.io please follow the instructions at:
    https://some.server/oauth/authorize?oauth_token=b_Tf-y1yXrPbfkOXj-PhFQ

Callbacks
---------

The callbacks work by you define a callback for when PyPump would like to be
called with the `url` when PyPump would like you to ask the user for the verifier
the prototype of the function is::

    def ask_verifier(self, url):
        """ This should display the url and ask the user to authorize your app """
        pass
        # pypump ignores the return value

Once you have the verifier you need to call `verifier`::

    >>> verifier
    c_Fe-91tXrPbfkOXj-xKFu
    >>> pypump_instance.verifier(verifier)


Example
-------

I wrote this, it's not a real GUI framework, I didn't want to weight this down
with a lot of other cruft, this is just to show a rough example of how this might
work::

    from pypump import PyPump
    from gui_framework import Window, Widgets    

    class MyGui(object):

        pump = None

        def __init__(self):
            self.window = Window()
            self.window.add(
                Widgets.Message("Checking authorization...")
                )

        def return_verifier(self):
            """ Hands the verifier back to PyPump """
            if self.pump is None:
                raise Exception("You need to set PyPump")

            verifier = self.verifier.get()
            self.pump.verifier(verifier)
            # Done!

        def ask_verifier(self, url):
            """ Will display a message with URL and a text box for the verifier """
            self.window.clear()
            self.verifier = Widgets.Textbox()
            self.button = Widgets.Button()
            self.window.add(
                Widgets.Message("Please authorize me!"),
                Widgets.Message(url),
                self.verifier,
                button,
                )
            self.button.when_clicked(self.return_verifier)


    gui = MyGui()

    pump = PyPump(
        "someome@server.org",
        client_name="MyClient",
        verifier_callback=gui.ask_verifier
        )
    gui.pump = pump