Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Command-line tools for authenticating via OAuth 2.0 |
| 16 | |
| 17 | Do the OAuth 2.0 Web Server dance for |
| 18 | a command line application. Stores the generated |
| 19 | credentials in a common file that is used by |
| 20 | other example apps in the same directory. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | __all__ = ["run"] |
| 25 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 26 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame^] | 27 | def run(flow, storage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 28 | """Core code for a command-line application. |
| 29 | """ |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame^] | 30 | authorize_url = flow.step1_get_authorize_url('oob') |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 31 | |
| 32 | print 'Go to the following link in your browser:' |
| 33 | print authorize_url |
| 34 | print |
| 35 | |
Joe Gregorio | 49e94d8 | 2011-01-28 16:36:13 -0500 | [diff] [blame] | 36 | accepted = 'n' |
| 37 | while accepted.lower() == 'n': |
| 38 | accepted = raw_input('Have you authorized me? (y/n) ') |
| 39 | code = raw_input('What is the verification code? ').strip() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 40 | |
| 41 | credentials = flow.step2_exchange(code) |
| 42 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame^] | 43 | storage.put(credentials) |
| 44 | credentials.set_store(storage.put) |
| 45 | |
| 46 | print 'You have successfully authenticated.' |
| 47 | |
| 48 | return credentials |