Esempio n. 1
0
 def test_for_failure(self):
     crypt.Signer = crypt.PyCryptoSigner
     private_key = datafile("privatekey.p12")
     credentials = SignedJwtAssertionCredentials(
         "*****@*****.**", private_key, scope="read+write", sub="*****@*****.**"
     )
     try:
         credentials._generate_assertion()
         self.fail()
     except NotImplementedError:
         pass
Esempio n. 2
0
 def test_for_failure(self):
     crypt.Signer = crypt.PyCryptoSigner
     private_key = datafile('privatekey.p12')
     credentials = SignedJwtAssertionCredentials('*****@*****.**',
                                                 private_key,
                                                 scope='read+write',
                                                 sub='*****@*****.**')
     try:
         credentials._generate_assertion()
         self.fail()
     except NotImplementedError:
         pass
 def test_for_failure(self):
   crypt.Signer = crypt.PyCryptoSigner
   private_key = datafile('privatekey.p12')
   credentials = SignedJwtAssertionCredentials(
       '*****@*****.**',
       private_key,
       scope='read+write',
       prn='*****@*****.**')
   try:
     credentials._generate_assertion()
     self.fail()
   except NotImplementedError:
     pass
Esempio n. 4
0
class OAuth2EventsUtility(object):
    flow = None
    credentials = None
    http_auth = None
    service = None
    PROJECT_NUMBER = None
    KEY_FILE = None
    SERVICE_ACCOUNT_EMAIL = None

    def __init__(self, **kwargs):
        self.PROJECT_NUMBER = kwargs.get('projectNumber', '774436620412')
        self.KEY_FILE = kwargs.get('keyFile', 'ssl/antenna_events.p12')
        self.SERVICE_ACCOUNT_EMAIL = kwargs.get(
            'serviceEmail',
            '*****@*****.**'
        )
        self.authorize()

    def authorize(self):
        with open(self.KEY_FILE, 'r') as fd:
            key = fd.read()

        self.credentials = SignedJwtAssertionCredentials(
            self.SERVICE_ACCOUNT_EMAIL,
            key,
            scope="https://www.googleapis.com/auth/bigquery")

        assertion = self.credentials._generate_assertion()

        self.http_auth = self.credentials.authorize(httplib2.Http())
        self.service = build('bigquery', 'v2', http=self.http_auth)

    def refresh(self):
        self.credentials._do_refresh_request(self.http_auth.request)

    def get_top_reaction_view_hash_counts(self,
                                          group,
                                          start,
                                          end,
                                          maxResults=100):
        query = 'select ch, count(ch) as counts  from %s where ch != "null" and et = "rs" and ev="rd" and %s group by ch order by counts desc'
        body = self.get_request_body(
            self.mod_query_for_dates(query, group, start, end), maxResults)
        try:
            result = self.service.jobs().query(projectId=int(
                self.PROJECT_NUMBER),
                                               body=body).execute()
            rows = result['rows']
            hash_tuples = []
            for row in rows:
                hash_tuples.append(
                    (row['f'][0]['v'], row['f'][1]['v']))  #hash, count
            return hash_tuples
        except Exception, ex:
            logger.warn(ex)
Esempio n. 5
0
#!/usr/bin/python

from oauth2client.client import SignedJwtAssertionCredentials
import json
import urllib
import urllib2

# Settings
json_key_file = '/home/pi/google_key/client_secrets.json'

# Load the private key associated with the Google service account
with open(json_key_file) as json_file:
    json_data = json.load(json_file)

# Get and sign JWT
credential = SignedJwtAssertionCredentials(json_data['client_email'], json_data['private_key'], 'https://www.googleapis.com/auth/devstorage.read_write')
jwt_complete = credential._generate_assertion()

# Get token from server
data = {'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer', 
    'assertion': jwt_complete}
f = urllib2.urlopen("https://accounts.google.com/o/oauth2/token", urllib.urlencode(data))

print f.read()

f = urllib2.urlopen("https://ctrlrpi.appspot.com")
print f.read()

Esempio n. 6
0
import json
import requests
import datetime

with open('Google-service.json') as json_file:
    json_data = json.load(json_file)

credential = SignedJwtAssertionCredentials(
    json_data['client_email'], json_data['private_key'],
    'https://www.googleapis.com/auth/calendar')

r2 = requests.post("https://accounts.google.com/o/oauth2/token",
                   data={
                       'grant_type':
                       'urn:ietf:params:oauth:grant-type:jwt-bearer',
                       'assertion': credential._generate_assertion()
                   })
accessToken = r2.json()['access_token']

######################

date = datetime.datetime.today().strftime('%Y-%m-%d')
print("Today is " + date)
CalendarID = "*****@*****.**"
r = requests.get("https://www.googleapis.com/calendar/v3/calendars/" +
                 CalendarID + "/events?timeMin=" + date +
                 "T00:00:00Z&timeMax=" + date + "T23:59:59Z",
                 headers={'Authorization': 'Bearer ' + accessToken})
print(str(r.json()))

Events = r.json()['items']