blob: 644e72b5c8a93a1dd9ba1febc3da30201a1c9601 [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001# 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
17Do the OAuth 2.0 Web Server dance for
18a command line application. Stores the generated
19credentials in a common file that is used by
20other example apps in the same directory.
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24__all__ = ["run"]
25
Joe Gregorio695fdc12011-01-16 16:46:55 -050026
Joe Gregoriodeeb0202011-02-15 14:49:57 -050027def run(flow, storage):
Joe Gregorio695fdc12011-01-16 16:46:55 -050028 """Core code for a command-line application.
29 """
Joe Gregoriodeeb0202011-02-15 14:49:57 -050030 authorize_url = flow.step1_get_authorize_url('oob')
Joe Gregorio695fdc12011-01-16 16:46:55 -050031
32 print 'Go to the following link in your browser:'
33 print authorize_url
34 print
35
Joe Gregorio49e94d82011-01-28 16:36:13 -050036 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 Gregorio695fdc12011-01-16 16:46:55 -050040
41 credentials = flow.step2_exchange(code)
42
Joe Gregoriodeeb0202011-02-15 14:49:57 -050043 storage.put(credentials)
44 credentials.set_store(storage.put)
45
46 print 'You have successfully authenticated.'
47
48 return credentials