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
Esempio n. 3
0
    def __init__(self):
        authorization_data = AuthorizationData(
            developer_token=config.developer_token(),
            authentication=OAuthAuthorization(client_id=config.oauth2_client_id(),
                                              oauth_tokens=config.developer_token()),
        )

        self.client = super(BingReportClient, self).__init__(service='ReportingService',
                                                             authorization_data=authorization_data,
                                                             environment='production', version='v12')