Created
July 3, 2014 13:26
-
-
Save feczo/c8e008e0a294080b4748 to your computer and use it in GitHub Desktop.
Example of using a service account in both production and development with a .pem key converted from devconsole
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webapp2 | |
from apiclient.discovery import build | |
from apiclient.errors import HttpError | |
import httplib2 | |
from oauth2client.client import SignedJwtAssertionCredentials | |
# Enter your Google Developer Project number | |
PROJECT_NUMBER = "XXXXXXXXXX" | |
class main(webapp2.RequestHandler): | |
def get(self): | |
# Load the key in PEM format that you downloaded from the Google API | |
# Console when you created your Service account. Convert with: | |
# openssl pkcs12 -passin pass:notasecret -in privatekey.p12 -nocerts \ | |
# -passout pass:notasecret -out key.pem | |
# openssl pkcs8 -nocrypt -in key.pem -passin pass:notasecret -topk8 \ | |
# -out privatekey.pem; rm key.pem | |
f = file("privatekey.pem", "rb") | |
key = f.read() | |
f.close() | |
# Create an httplib2.Http object to handle HTTP requests and authorize it | |
# with the Credentials. Note that the first parameter, service_account_name, | |
# is the Email address created for the Service account. It must be the email | |
# address associated with the key that was created. | |
credentials = SignedJwtAssertionCredentials( | |
"XXXXXXXXXX-HASH" | |
"@developer.gserviceaccount.com", | |
key, | |
scope="https://www.googleapis.com/auth/bigquery") | |
http = httplib2.Http() | |
http = credentials.authorize(http) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment