Beispiel #1
0
    def exchange_code_for_access_token(self, env_type, code):
        logging.info("Trying to get a new user access token ... ")
        credential = credentialutil.get_credentials(env_type)

        headers = oauthclient.model.util._generate_request_headers(credential)
        body = oauthclient.model.util._generate_oauth_request_body(
            credential, code)
        resp = requests.post(env_type.api_endpoint, data=body, headers=headers)

        content = json.loads(resp.content)
        token = oAuth_token()

        if resp.status_code == requests.codes.ok:
            token.access_token = content['access_token']
            token.token_expiry = datetime.utcnow() + timedelta(
                seconds=int(content['expires_in'])) - timedelta(minutes=5)
            token.refresh_token = content['refresh_token']
            token.refresh_token_expiry = datetime.utcnow() + timedelta(
                seconds=int(content['refresh_token_expires_in'])) - timedelta(
                    minutes=5)
        else:
            token.error = str(
                resp.status_code) + ': ' + content['error_description']
            logging.error("Unable to retrieve token.  Status code: %s - %s",
                          resp.status_code,
                          requests.status_codes._codes[resp.status_code])
            logging.error("Error: %s - %s", content['error'],
                          content['error_description'])
        return token
Beispiel #2
0
    def get_application_token(self, env_type, scopes):
        """
            makes call for application token and stores result in credential object
            returns credential object
        """

        logging.info("Trying to get a new application access token ... ")
        credential = credentialutil.get_credentials(env_type)
        headers = oauthclient.model.util._generate_request_headers(credential)
        body = oauthclient.model.util._generate_application_request_body(
            credential, ' '.join(scopes))

        resp = requests.post(env_type.api_endpoint, data=body, headers=headers)
        content = json.loads(resp.content)
        token = oAuth_token()

        if resp.status_code == requests.codes.ok:
            token.access_token = content['access_token']
            # set token expiration time 5 minutes before actual expire time
            token.token_expiry = datetime.utcnow() + timedelta(
                seconds=int(content['expires_in'])) - timedelta(minutes=5)

        else:
            token.error = str(
                resp.status_code) + ': ' + content['error_description']
            logging.error("Unable to retrieve token.  Status code: %s - %s",
                          resp.status_code,
                          requests.status_codes._codes[resp.status_code])
            logging.error("Error: %s - %s", content['error'],
                          content['error_description'])
        return token
Beispiel #3
0
    def generate_user_authorization_url(self, env_type, scopes, state=None):
        '''
            env_type = environment.SANDBOX or environment.PRODUCTION
            scopes = list of strings
        '''

        credential = credentialutil.get_credentials(env_type)

        scopes = ' '.join(scopes)
        param = {
            'client_id': credential.client_id,
            'redirect_uri': credential.ru_name,
            'response_type': 'code',
            'prompt': 'login',
            'scope': scopes
        }

        if state != None:
            param.update({'state': state})

        query = urllib.parse.urlencode(param)
        return env_type.web_endpoint + '?' + query
Beispiel #4
0
"""Used after you have refresh tokens for ebay business accounts received by ebay-oauth-python-client app.
This 'UpdateTrackings' app queries tracking information of orders and then fills up this information to google sheets
where all required information for business managers is stored"""

import refresh_token
from cmd_args_settings import EbaySettings, GSheetsSettings, args
from oauthclient.model.model import environment
import ebay_manager
from oauthclient.credentialutil import credentialutil
import update_google_sheet

if __name__ == "__main__":
    #credentials are used in oauth2api() and get_ebay_orders
    credentialutil.load(args.dev_creds_path)
    dev_credentials = credentialutil.get_credentials(environment.PRODUCTION)

    email2acc_token = refresh_token.get_access_tokens(args.refr_tokens_path,
                                                      environment.PRODUCTION,
                                                      EbaySettings.app_scopes)
    order_id2tracking = {}
    for email, access_token in email2acc_token.items():
        order_id2tracking.update(
            ebay_manager.get_order2tracking(dev_credentials, access_token))
    update_google_sheet.update(
        GSheetsSettings.DEFAULT_GSHEETS_CREDENTIALS_PATH, order_id2tracking)