OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 '''A set of utilities to interface with the Chrome Webstore API.''' |
| 8 |
| 9 import SimpleHTTPServer |
| 10 import SocketServer |
| 11 import httplib |
| 12 import json |
| 13 import os |
| 14 import re |
| 15 import sys |
| 16 import thread |
| 17 import urllib |
| 18 import webbrowser |
| 19 |
| 20 PROJECT_ARGS = { |
| 21 'client_id': ('937534751394-gbj5334v9144c57qjqghl7d283plj5r4' |
| 22 '.apps.googleusercontent.com'), |
| 23 'grant_type': 'authorization_code', |
| 24 'redirect_uri': 'http://localhost:8000' |
| 25 } |
| 26 |
| 27 PORT = 8000 |
| 28 |
| 29 APP_ID = 'kgejglhpjiefppelpmljglcjbhoiplfn' |
| 30 OAUTH_DOMAIN = 'accounts.google.com' |
| 31 OAUTH_AUTH_COMMAND = '/o/oauth2/auth' |
| 32 OAUTH_TOKEN_COMMAND = '/o/oauth2/token' |
| 33 WEBSTORE_API_SCOPE = 'https://www.googleapis.com/auth/chromewebstore' |
| 34 |
| 35 API_ENDPOINT_DOMAIN = 'www.googleapis.com' |
| 36 COMMAND_GET_UPLOAD_STATUS = ( |
| 37 '/chromewebstore/v1.1/items/%s?projection=draft' % APP_ID) |
| 38 COMMAND_POST_PUBLISH = '/chromewebstore/v1.1/items/%s/publish' % APP_ID |
| 39 COMMAND_POST_UPLOAD = '/upload/chromewebstore/v1.1/items/%s' % APP_ID |
| 40 |
| 41 class CodeRequestHandler(SocketServer.StreamRequestHandler): |
| 42 def handle(self): |
| 43 content = self.rfile.readline() |
| 44 self.server.code = re.search('code=(.*) ', content).groups()[0] |
| 45 self.rfile.close() |
| 46 |
| 47 def GetAuthCode(): |
| 48 Handler = CodeRequestHandler |
| 49 httpd = SocketServer.TCPServer(("", PORT), Handler) |
| 50 query = '&'.join(['response_type=code', |
| 51 'scope=%s' % WEBSTORE_API_SCOPE, |
| 52 'client_id=%(client_id)s' % PROJECT_ARGS, |
| 53 'redirect_uri=%(redirect_uri)s' % PROJECT_ARGS]) |
| 54 auth_url = ' https://%s%s?%s' % (OAUTH_DOMAIN, OAUTH_AUTH_COMMAND, query) |
| 55 print 'Navigating to %s' % auth_url |
| 56 webbrowser.open(auth_url) |
| 57 httpd.handle_request() |
| 58 return httpd.code |
| 59 |
| 60 def GetOauthToken(code, client_secret): |
| 61 PROJECT_ARGS['code'] = code |
| 62 PROJECT_ARGS['client_secret'] = client_secret |
| 63 body = urllib.urlencode(PROJECT_ARGS) |
| 64 conn = httplib.HTTPSConnection(OAUTH_DOMAIN) |
| 65 conn.putrequest('POST', OAUTH_TOKEN_COMMAND) |
| 66 conn.putheader('content-type', 'application/x-www-form-urlencoded') |
| 67 conn.putheader('content-length', len(body)) |
| 68 conn.endheaders() |
| 69 conn.send(body) |
| 70 content = conn.getresponse().read() |
| 71 return json.loads(content) |
| 72 |
| 73 def GetPopulatedHeader(client_secret): |
| 74 code = GetAuthCode() |
| 75 access_token = GetOauthToken(code, client_secret) |
| 76 url = 'www.googleapis.com' |
| 77 |
| 78 return {'Authorization': 'Bearer %(access_token)s' % access_token, |
| 79 'x-goog-api-version': 2, |
| 80 'Content-Length': 0 |
| 81 } |
| 82 |
| 83 def SendGetCommand(command, client_secret): |
| 84 headers = GetPopulatedHeader(client_secret) |
| 85 conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN) |
| 86 conn.request('GET', command, '', headers) |
| 87 return conn.getresponse() |
| 88 |
| 89 def SendPostCommand(command, client_secret, header_additions = {}, body=None): |
| 90 headers = GetPopulatedHeader(client_secret) |
| 91 headers = dict(headers.items() + header_additions.items()) |
| 92 conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN) |
| 93 conn.request('PUT', command, body, headers) |
| 94 return conn.getresponse() |
| 95 |
| 96 def GetUploadStatus(client_secret): |
| 97 '''Gets the status of a previous upload. |
| 98 Args: |
| 99 client_secret ChromeVox's client secret creds. |
| 100 ''' |
| 101 return SendGetCommand(COMMAND_GET_UPLOAD_STATUS, client_secret) |
| 102 |
| 103 # httplib fails to persist the connection during upload; use curl instead. |
| 104 def PostUpload(file, client_secret): |
| 105 '''Posts an uploaded version of ChromeVox. |
| 106 Args: |
| 107 file A string path to the ChromeVox extension zip. |
| 108 client_secret ChromeVox's client secret creds. |
| 109 ''' |
| 110 header = GetPopulatedHeader(client_secret) |
| 111 curl_command = ' '.join(['curl', |
| 112 '-H "Authorization: %(Authorization)s"' % header, |
| 113 '-H "x-goog-api-version: 2"', |
| 114 '-X PUT', |
| 115 '-T %s' % file, |
| 116 '-v', |
| 117 'https://%s%s' % (API_ENDPOINT_DOMAIN, |
| 118 COMMAND_POST_UPLOAD)]) |
| 119 |
| 120 print 'Running %s' % curl_command |
| 121 if os.system(curl_command) != 0: |
| 122 sys.exit(-1) |
| 123 |
| 124 def PostPublishTrustedTesters(client_secret): |
| 125 '''Publishes a previously uploaded ChromeVox extension to trusted testers. |
| 126 Args: |
| 127 client_secret ChromeVox's client secret creds. |
| 128 ''' |
| 129 return SendPostCommand(COMMAND_POST_PUBLISH, |
| 130 client_secret, |
| 131 { 'publishTarget': 'trustedTesters'}) |
| 132 |
| 133 def PostPublish(client_secret): |
| 134 '''Publishes a previously uploaded ChromeVox extension publically. |
| 135 Args: |
| 136 client_secret ChromeVox's client secret creds. |
| 137 ''' |
| 138 return SendPostCommand(COMMAND_POST_PUBLISH, client_secret) |
OLD | NEW |