×

35+ Native Ad Campaigns
In Your Inbox

Your free lookbook with the best native ad examples awaits.

Join The Slack Community for Native Advertising Experts>> Join the Community

The Yahoo API OAuth Authentication Process Guide & Example

You’re trying to connect to the Yahoo Gemini API (Verizon Media Native), or any of the other Yahoo API services but first need to go through authentication. Following is our step by step guide with examples of how to do the Yahoo API Oauth authentication process.


How to connect to the Yahoo Gemini API for native advertisers using python

This article is mainly directed at marketers trying to connect to the Verizon Media Native API (previously Yahoo Gemini API), whether for reporting or doing campaign/ad changes. However the process should be similar for any of the Yahoo API services. So regardless if you are trying to connect to your advertiser account or just get stuck on the Oauth process. This guide is for you.

We have also in previous articles covered the Taboola API authentication, how to connect to the Outbrain API and getting started with Verizon Media Native. Check them out as well if you are working with these platforms.

Let’s get started with the Yahoo API Oauth authentication process.

Firstly… Apply for access

Unfortunately, there are a few prerequisites before we can really get started. First one you have to apply for access to use API. So if you haven’t already applied and been granted access, you can do it through this link.

For this step, you need to already have a Yahoo account. If you don’t already have one, it can be created here.

Getting access from this application is generally quick and you should be ready to continue within a few hours.

Secondly… Create an App?

Not any happier about this next step, but it’s at least the last thing we need to set up before we can work on the actual API. Go here to create a Yahoo App. Make sure you’re logged into the same account as you used in applying for API access.

Pick whatever you’d like for the Application Name, and make sure that you have selected Installed Application under application type. You can skip the next fields up until the Redirect URI(s). Here you can use anything that looks like a proper url, e.g. https://example.com. Does not need to be an actual web address. Under API Permissions the Read/Write should be selected for Oauth Ad Platforms. It should look something like the screenshot below.


Yahoo API Oauth App creation example

Then click the button to create the app. Provided you don’t get any errors, there should now be an App ID, Client ID and Client Secret. Make sure you save these somewhere where you can find them again. Then you never really need to return here again.

Now can we really start?

Yes, yes we can. Next up is the actual authentication process using Oauth 2.0. If you are curious to what that is, check out the Yahoo documentation on Oauth 2.0.

Start up whichever IDE you are using for running Python, in this guide we’ll be using Jupyter Notebook (instructions for installing) but anything else will also work. Then import the following packages:

from requests import get, post
import json
import webbrowser
import base64

Next let’s define the Client ID, Client Secret and the url we will use for the authentication requests.

client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
base_url = 'https://api.login.yahoo.com/'

Your code should now look something like the following.

Yahoo API Oauth example parameters defined

Manually approve application access and receive the Authentication Code

Now it’s time to approve that the application we created in a previous step can access the Verizon Media Native (Yahoo Gemini) Platform on our behalf.

First define the url for the approval of app access:

code_url = f'oauth2/request_auth?client_id={client_id}&redirect_uri=oob&response_type=code&language=en-us'

Then prompt the approval page by running the following:

webbrowser.open(base_url + code_url)

You should now see the following authentication approval page in your browser.


Yahoo API Oauth Authentication approval

If you don’t, get and copy the exact url into browser using:

print(base_url + code_url)

Click Agree and you’ll get directed to a confirmation page, see screenshot. This is where you find the Authentication Code.


Yahoo API Oauth Authentication approved and code

Make sure to save and define the Authentication Code to your script and then move on to the next step.

code = 'xxxxxxx'

The script so far would look like this:

Yahoo API Oauth Authentication Code

Exchange Authentication Code for Refresh and Access Token

With the code in hand, we can finally request the tokens that will allow us to do whatever we actually want to do with the API. Copy the following code to define the parameters needed for this:

encoded = base64.b64encode((client_id + ':' + client_secret).encode("utf-8"))
headers = {
    'Authorization': f'Basic {encoded.decode("utf-8")}',
    'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
    'grant_type': 'authorization_code',
    'redirect_uri': 'oob',
    'code': code
}

And then request the tokens.

response = post(base_url + 'oauth2/get_token', headers=headers, data=data)

Let’s check if the request worked or not. If response.ok is returning True then you will find the Access Token and Refresh Token in response.json().

response.ok
response.json()

This should produce something like the following output.

Yahoo API Oauth example output

If it does, you have successfully gone through the Yahoo OAuth Authentication process. Let’s make sure to save both tokens for later use.

access_token = response.json()['access_token']
refresh_token = response.json()['refresh_token']

The Access Token is what you will use for pulling reporting data from the API, making campaigns changes etc. It is however only valid for an hour from the point it was generated and will then need to be renewed. Luckily you don’t need to go through this entire process again to generate a new one. This is what you will save and use the Refresh Token, which doesn’t expire.

Slack conversion element

Generating new Access Token using the Refresh Token

So now you could actually go ahead and start making direct request to the Verizon Media Native API. But let’s quickly cover how you generate a new Access Token once the current one expires.

Simply update the previously defined data parameters and run the request again:

data = {
    'grant_type': 'refresh_token',
    'redirect_uri': 'oob',
    'code': code,
    'refresh_token': refresh_token
}
response = post(base_url + 'oauth2/get_token', headers=headers, data=data)

Then, provided the response.ok still returns True, save the updated Access Token.

access_token = response.json()['access_token']

See script below for using the Refresh Token to generate a new Access Token.

Yahoo API Oauth refresh token for access token example output

Testing the Access Token

Finally time to make sure that all the effort we went through actually paid off. Let’s see if we properly went through the Yahoo API OAuth authentication and can now make direct requests to the native API.

The headers that you will use with every request should now include the Access Token and look like:

headers = {
    'Authorization': f'Bearer {access_token}',
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}

Then let’s see which Advertisers are connected to your Yahoo account.

response = get('https://api.gemini.yahoo.com/v3/rest/advertiser/', headers=headers)

As usual, if response.ok returns True the call worked. Meaning that the Access Token worked like it should and the API now is open for any request you would like to make. To see the actual Verizon Media Native advertiser accounts use:

response.json()['response']

The output should look something like:

Yahoo API Oauth test output

No more next steps, all you now!

Hopefully if you made it to this point, you got the same results and your authentication is now sorted. What you now choose to do with the API is completely up to you.

A logical place to start looking is the official documentation for the Verizon Media Native API. You’ll find information about using the API for reporting here and for campaign management here.

If you are looking for better ways of handling reporting and managing your advertising of your Yahoo Gemini campaigns check out our Native Ads Management Platform. We’re offering powerful alerts, automation and data structuring tools across most Native Advertising platforms.