|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +''' |
| 4 | +Helper to generate OVH API consumer key. In a nutshell, the consumer key |
| 5 | +identifies a specific user in an application while application key and |
| 6 | +application secret identifies the application itself. In the case of ovh-cli |
| 7 | +each instance of the CLI must hav its own, dedicated, set of credentials. |
| 8 | +
|
| 9 | +To generate application secret and application key, please visit: |
| 10 | + - OVH Europe: https://eu.api.ovh.com/createApp/ |
| 11 | + - OVH North America: https://ca.api.ovh.com/createApp/ |
| 12 | + - Soyoustart Europe: https://eu.api.soyoustart.com/createApp/ |
| 13 | + - Soyoustart North America: https://ca.api.soyoustart.com/createApp/ |
| 14 | + - Kimsufi Europe: https://eu.api.kimsufi.com/createApp/ |
| 15 | + - Kimsufi North America: https://ca.api.kimsufi.com/createApp/ |
| 16 | + - Runabove North America: https://api.runabove.com/createApp/ |
| 17 | +
|
| 18 | +You may then request a consumer key using this tool: |
| 19 | +
|
| 20 | + $ create-consumer-key.py [endpoint] |
| 21 | +
|
| 22 | +Where ``endpoint`` may be one of ``ovh-eu``, ``ovh-ca``, and so on. |
| 23 | +
|
| 24 | +Once generated, your application key, application secret and consumer key |
| 25 | +must be set in eiter: |
| 26 | + - ``./ovh.conf`` for an application specific configuration |
| 27 | + - ``$HOME/.ovh.conf`` for a user wide configuration |
| 28 | + - ``/etc/ovh.conf`` for a system wide / server configuration |
| 29 | +
|
| 30 | +This file will look like: |
| 31 | +
|
| 32 | + [default] |
| 33 | + endpoint=ovh-eu |
| 34 | +
|
| 35 | + [ovh-eu] |
| 36 | + application_key=my_app_key |
| 37 | + application_secret=my_application_secret |
| 38 | + ;consumer_key=my_consumer_key |
| 39 | +
|
| 40 | +Alternatively, at runtime, configuration may be overloaded using environment |
| 41 | +variables. For more informations regarding available configuration options, |
| 42 | +please see https://github.com/ovh/python-ovh |
| 43 | +''' |
| 44 | + |
| 45 | +import sys |
| 46 | +import ovh |
| 47 | + |
| 48 | +# Load api endpoint from command line, if any |
| 49 | +if len(sys.argv) == 1: |
| 50 | + endpoint=None |
| 51 | +elif len(sys.argv) == 2: |
| 52 | + endpoint=sys.argv[1] |
| 53 | +else: |
| 54 | + print >>sys.stderr, __doc__ |
| 55 | + sys.exit(1) |
| 56 | + |
| 57 | +if endpoint in ['-h', '--help']: |
| 58 | + print >>sys.stderr, __doc__ |
| 59 | + sys.exit(0) |
| 60 | + |
| 61 | +# Create a client using configuration |
| 62 | +try: |
| 63 | + client = ovh.Client(endpoint) |
| 64 | +except Exception as e: |
| 65 | + print e |
| 66 | + print >>sys.stderr, __doc__ |
| 67 | + sys.exit(1) |
| 68 | + |
| 69 | +# Request full API access |
| 70 | +access_rules = [ |
| 71 | + {'method': 'GET', 'path': '/*'}, |
| 72 | + {'method': 'POST', 'path': '/*'}, |
| 73 | + {'method': 'PUT', 'path': '/*'}, |
| 74 | + {'method': 'DELETE', 'path': '/*'} |
| 75 | +] |
| 76 | + |
| 77 | +# Request token |
| 78 | +validation = client.request_consumerkey(access_rules) |
| 79 | + |
| 80 | +print "Please visit %s to authenticate" % validation['validationUrl'] |
| 81 | +raw_input("and press Enter to continue...") |
| 82 | + |
| 83 | +# Print nice welcome message |
| 84 | +print "Welcome", client.get('/me')['firstname'] |
| 85 | +print "Here is your Consumer Key: '%s'" % validation['consumerKey'] |
| 86 | + |
0 commit comments