MicrosoftDocs / Advertising-docs

Creative Commons Attribution 4.0 International
15 stars 59 forks source link

Too difficult #478

Closed wilminkj closed 5 years ago

wilminkj commented 5 years ago

I am trying to connect to Bing Ads data by using python in a jupyter notebook. After a day I still haven't connected. I have a production token but I run into a dead end when requesting user consent. I am redirected to the follwoing error message: https://login.live.com/err.srf?lc=1043#error=invalid_request&error_description= The+provided+value+for+the+input+parameter+'redirect_uri'+is+not+valid.+The+expected+value+is+'https://login.live.com/oauth20_desktop.srf'+or+a+URL+which+matches+the+redirect+URI+registered+for+this+client+application.

My application obvioulsy doesn't have a "redirect URI" because I am just working from my notebook, so I can not set that. It would be a very nice improvement of the documentation if you could add some basic simple examples to get started.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

eric-urban commented 5 years ago

@wilminkj although we haven't published a Jupyter notebook example specifically, we have some OAuth samples that should help i.e., here, here, and here. Long story short, for a local app you should register a Native app and use https://login.live.com/oauth20_desktop.srf as the redirect URI. If you are using the Bing Ads Python SDK just use an OAuthDesktopMobileAuthCodeGrant instance and formatting of the scope, redirect_uri, etc should be taken care of. With a localhost web notebook then you could alternatively register https://localhost:portnumber and use the application ID (client_id) and password (client_secret) with the OAuthWebAuthCodeGrant instance.

We will also update this page to link to the samples. This Authentication with OAuth guide describes the grant flow details, and we should at least link to the quick start. :)

Also for more visibility if you share your Jupyter repro steps in the API forum, I'm happy to continue the conversation there.

I did get this example working in a Jupyter notebook.

  1. Install the SDK:
import sys
!{sys.executable} -m pip install --upgrade --no-cache-dir bingads

jupyter-install-bingads

  1. Add the sample code from here with your client_id (Application Id) and DeveloperToken (for production or sandbox).

    Optionally you can include logging to output traffic, for example the SOAP request and response.

    import logging
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('suds.client').setLevel(logging.DEBUG)
    logging.getLogger('suds.transport.http').setLevel(logging.DEBUG)
  2. Click Run, and you'll be prompted for consent of your app to manage your Bing Ads accounts. Copy the result URL with code=xyz back into your notebook and press 'Enter'.

jupyter

By default the refresh.txt file is stored in your local directory by default so please be sure to store it securely.

I hope this helps!