dspassion
dspassion

Reputation: 41

Bing Ads API on Linux

Does anyone know if we could run Bing Ads API with Python in Linux? We currently have the process running on Windows to generate flat files but we would like to migrate this Python script to Linux. I tried running my get_started.py script and it gave me the error I shared below, there isn't any documentation related to this so I am not sure if this is something we can do?

Documentation is here: https://learn.microsoft.com/en-us/advertising/guides/walkthrough-desktop-application-python?view=bingads-13

def authenticate(authorization_data):

    customer_service=ServiceClient(
        service='CustomerManagementService',
        version=13,
        authorization_data=authorization_data,
        environment=ENVIRONMENT,
    )

    # You should authenticate for Bing Ads API service operations with a Microsoft Account.
    authenticate_with_oauth(authorization_data)

    # Set to an empty user identifier to get the current authenticated Microsoft Advertising user,
    # and then search for all accounts the user can access.
    user=get_user_response=customer_service.GetUser(
        UserId=None
    ).User
    accounts=search_accounts_by_user_id(customer_service, user.Id)

    # For this example we'll use the first account.
    authorization_data.account_id=accounts['AdvertiserAccount'][0].Id
    authorization_data.customer_id=accounts['AdvertiserAccount'][0].ParentCustomerId


def authenticate_with_oauth(authorization_data):

    authentication=OAuthDesktopMobileAuthCodeGrant(
        client_id=CLIENT_ID,
        env=ENVIRONMENT
    )

    # It is recommended that you specify a non guessable 'state' request parameter to help prevent
    # cross site request forgery (CSRF).
    authentication.state=CLIENT_STATE

    # Assign this authentication instance to the authorization_data.
    authorization_data.authentication=authentication

    # Register the callback function to automatically save the refresh token anytime it is refreshed.
    # Uncomment this line if you want to store your refresh token. Be sure to save your refresh token securely.
    authorization_data.authentication.token_refreshed_callback=save_refresh_token

    refresh_token=get_refresh_token()

    try:
        # If we have a refresh token let's refresh it
        if refresh_token is not None:
            authorization_data.authentication.request_oauth_tokens_by_refresh_token(refresh_token)
        else:
            request_user_consent(authorization_data)
    except OAuthTokenRequestException:
        # The user could not be authenticated or the grant is expired.
        # The user must first sign in and if needed grant the client application access to the requested scope.
        request_user_consent(authorization_data)

Error shared here:

  Traceback (most recent call last):
  File "/usr/local/etl/Script/BING/get-started.py", line 72, in authenticate_with_oauth
    request_user_consent(authorization_data)
  File "/usr/local/etl/Script/BING/get-started.py", line 98, in request_user_consent
    authorization_data.authentication.request_oauth_tokens_by_response_uri(response_uri=response_uri)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/name/.local/lib/python3.11/site-packages/bingads/authorization.py", line 464, in request_oauth_tokens_by_response_uri
    self.token_refreshed_callback(self.oauth_tokens)  # invoke the callback when token refreshed.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/etl/Script/BING/get-started.py", line 119, in save_refresh_token
    with open(REFRESH_TOKEN,"w+") as file:
         ^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 36] File name too long: 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/etl/Script/BING/get-started.py", line 393, in <module>
    authenticate(authorization_data)
  File "/usr/local/etl/Script/BING/get-started.py", line 34, in authenticate
    authenticate_with_oauth(authorization_data)
  File "/usr/local/etl/Script/BING/get-started.py", line 73, in authenticate_with_oauth
    except OAuthTokenRequestException:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
NameError: name 'OAuthTokenRequestException' is not defined

    [name@xservername-etl02 BING]$ [ERROR viaduct::backend::ffi] Missing HTTP status
    [ERROR viaduct::backend::ffi] Missing HTTP status

Upvotes: 0

Views: 162

Answers (0)

Related Questions