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.
7 '''A set of utilities to interface with the Chrome Webstore API.'''
9 import SimpleHTTPServer
21 'client_id': ('937534751394-gbj5334v9144c57qjqghl7d283plj5r4'
22 '.apps.googleusercontent.com'),
23 'grant_type': 'authorization_code',
24 'redirect_uri': 'http://localhost:8000'
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'
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
41 class CodeRequestHandler(SocketServer
.StreamRequestHandler
):
43 content
= self
.rfile
.readline()
44 self
.server
.code
= re
.search('code=(.*) ', content
).groups()[0]
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()
61 def GetOauthToken(code
, client_secret
):
62 PROJECT_ARGS
['code'] = code
63 PROJECT_ARGS
['client_secret'] = client_secret
64 body
= urllib
.urlencode(PROJECT_ARGS
)
65 conn
= httplib
.HTTPSConnection(OAUTH_DOMAIN
)
66 conn
.putrequest('POST', OAUTH_TOKEN_COMMAND
)
67 conn
.putheader('content-type', 'application/x-www-form-urlencoded')
68 conn
.putheader('content-length', len(body
))
71 content
= conn
.getresponse().read()
72 return json
.loads(content
)
74 def GetPopulatedHeader(client_secret
):
76 access_token
= GetOauthToken(code
, client_secret
)
77 url
= 'www.googleapis.com'
79 return {'Authorization': 'Bearer %(access_token)s' % access_token
,
80 'x-goog-api-version': 2,
84 def SendGetCommand(command
, client_secret
):
85 headers
= GetPopulatedHeader(client_secret
)
86 conn
= httplib
.HTTPSConnection(API_ENDPOINT_DOMAIN
)
87 conn
.request('GET', command
, '', headers
)
88 return conn
.getresponse()
90 def SendPostCommand(command
, client_secret
, header_additions
= {}, body
=None):
91 headers
= GetPopulatedHeader(client_secret
)
92 headers
= dict(headers
.items() + header_additions
.items())
93 conn
= httplib
.HTTPSConnection(API_ENDPOINT_DOMAIN
)
94 conn
.request('POST', command
, body
, headers
)
95 return conn
.getresponse()
97 def GetUploadStatus(client_secret
):
98 '''Gets the status of a previous upload.
100 client_secret ChromeVox's client secret creds.
102 return SendGetCommand(COMMAND_GET_UPLOAD_STATUS
, client_secret
)
104 # httplib fails to persist the connection during upload; use curl instead.
105 def PostUpload(file, client_secret
):
106 '''Posts an uploaded version of ChromeVox.
108 file A string path to the ChromeVox extension zip.
109 client_secret ChromeVox's client secret creds.
111 header
= GetPopulatedHeader(client_secret
)
112 curl_command
= ' '.join(['curl',
113 '-H "Authorization: %(Authorization)s"' % header
,
114 '-H "x-goog-api-version: 2"',
118 'https://%s%s' % (API_ENDPOINT_DOMAIN
,
119 COMMAND_POST_UPLOAD
)])
121 print 'Running %s' % curl_command
122 if os
.system(curl_command
) != 0:
125 def PostPublishTrustedTesters(client_secret
):
126 '''Publishes a previously uploaded ChromeVox extension to trusted testers.
128 client_secret ChromeVox's client secret creds.
130 return SendPostCommand(COMMAND_POST_PUBLISH
,
132 { 'publishTarget': 'trustedTesters'})
134 def PostPublish(client_secret
):
135 '''Publishes a previously uploaded ChromeVox extension publically.
137 client_secret ChromeVox's client secret creds.
139 return SendPostCommand(COMMAND_POST_PUBLISH
, client_secret
)