def authenticate_with_oauth(api_client):
    """
    Sets the authentication with OAuthDesktopMobileAuthCodeGrant.
    Args:
        param api_client: The BingApiClient.
    """

    authentication = OAuthDesktopMobileAuthCodeGrant(
        client_id=config.oauth2_client_id()
    )

    api_client.authorization_data.authentication = authentication

    # load refresh token from config
    refresh_token = config.oauth2_refresh_token()
    try:
        # If we have a refresh token let's refresh it
        if refresh_token is not None:
            api_client.authorization_data.authentication.request_oauth_tokens_by_refresh_token(
                refresh_token)
        else:
            print('No refresh token found. Please run refresh refresh-bingads-api-oauth2-token')
            sys.exit(1)
    except OAuthTokenRequestException as exc:
        print('Authentication error. Could be necessary to run refresh refresh-bingads-api-oauth2-token',
              file=sys.stderr)
        print('Ensure that the registered application type is Native-Application', file=sys.stderr)
        print(' - client id: {}'.format(config.oauth2_client_id()), file=sys.stderr)
        print(' - refresh_token: {}'.format(refresh_token), file=sys.stderr)
        print(exc, file=sys.stderr)
        sys.exit(1)
def refresh_oauth_token():
    """Retrieve and display the access and refresh token."""
    """
    Search for account details by UserId.
    Args:
        param api_client: The BingApiClient.
    Returns:
        List of accounts that the user can manage.
    """
    api_client = BingReportClient()
    authentication = OAuthDesktopMobileAuthCodeGrant(client_id=config.oauth2_client_id())
    api_client.authorization_data.authentication = authentication
    print(
        'Authorization Endpoint: {}'.format(api_client.authorization_data.authentication.get_authorization_endpoint()))
    webbrowser.open(api_client.authorization_data.authentication.get_authorization_endpoint(),
                    new=1)
    response_uri = input(
        "You need to provide consent for the application to access your Bing Ads accounts. "
        "After you have granted consent in the web browser for the application to access your Bing Ads accounts, "
        "please enter the response URI that includes the authorization 'code' parameter: \n"
    )

    # Request access and refresh tokens using the URI that you provided manually during program execution.
    oauth_tokens = api_client.authorization_data.authentication.request_oauth_tokens_by_response_uri(
        response_uri=response_uri)
    print('Below is your oauth refresh token:')
    print(str(oauth_tokens.refresh_token).replace('!', '\!'))  # this is important for bash
Exemple #3
0
def create_sdk_client(service, account_id):
    LOGGER.info('Creating SOAP client with OAuth refresh credentials for service: %s, account_id %s',
                service, account_id)

    authentication = OAuthDesktopMobileAuthCodeGrant(
        client_id=CONFIG['oauth_client_id'],
        env='production'
    ) ## redirect URL not needed for refresh token

    authentication.request_oauth_tokens_by_refresh_token(CONFIG['refresh_token'])

    authorization_data = AuthorizationData(
        account_id=account_id,
        customer_id=CONFIG['customer_id'],
        developer_token=CONFIG['developer_token'],
        authentication=authentication)

    return CustomServiceClient(service, authorization_data=authorization_data)