Google API Authorization (Service Account) Error: HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request
<p>I'm attempting to connect to the YouTube Analytics API using Python.  When I go to Google's Developer Console in the Credentials tab I click on the <code>Create credentials</code> drop-down menu and select <code>Help me choose</code>.  I click on the API I want to use (<code>YouTube Analytics API</code>), where I will be calling from (<code>Other non-UI (e.g. cron job, daemon)</code>), what data I will be accessing (<code>Application Data</code>), and then whether I'm using Google App Engine (<code>no</code>). I click on the button to see which credentials I need and it tells me <code>You alread have credentials that are suitable for this purpose</code>.</p>

<p>I have a <code>Service account</code> that I use to connect to the Google Search Console API to access data for multiple sites my company owns. Because we have multiple sites I use delegated credentials based on my email address.  This is the code I use to authenticate to the Search Console API:</p>

<pre><code>from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build

scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)

delegated_credentials = credentials.create_delegated('&lt;my-email&gt;')
http_auth = delegated_credentials.authorize(Http())

webmasters_service = build('webmasters', 'v3', http=http_auth)
</code></pre>

<p>Now, I'm trying to use a similar approach with the YouTube Analytics API but I'm getting this error: <code>HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.</code>.  Here's my code:</p>

<pre><code>from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build

START_DATE = "2016-09-01"
END_DATE = "2016-09-30"

scopes = ['https://www.googleapis.com/auth/yt-analytics.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)

delegated_credentials = credentials.create_delegated('&lt;my-email&gt;')
http_auth = delegated_credentials.authorize(Http())

youtube_service = build('youtubeAnalytics', 'v1', http=http_auth)

analytics_query_response = youtube_service.reports().query(
    ids="channel==&lt;my-youtube-channel-id&gt;",
    metrics="views",
    start_date=START_DATE,
    end_date=END_DATE
).execute()
</code></pre>

<p>The <code>keyfile.json</code> is the same file (containing the Service account credentials) that I use to connect to the Search Console API. I even tried created a new Service account and used those credentials but I didn't have any luck. And yes, I've enabled the all of the YouTube APIs in the developer console.</p>

<p><strong>Do you have any idea why I'm getting the <code>HttpAccessTokenRefreshError: unauthorized_client: ...</code> error?</strong></p>

<p>edit: Previously I used an OAuth ID as opposed to a Service account, when I ran my script a browser tab was presented and I had to choose an account. I was presented with two options: one was my email, the other was the YouTube account that I was added as a manager. Do you think that I'm getting that error because I'm using my email address to generate credentials (and not the YouTube account)?</p>

<p>edit2: It appears that the YouTube account may not fall under the umbrella of our Google Apps domain. So, this may be why I can't authorize using my email address, even though I've been made a Manager of that YouTube account with that email address.</p>

<p>I found on this <a href="https://developers.google.com/youtube/analytics/authentication" rel="nofollow">documentation</a> that you cannot use Service account with YouTube Analytics API.</p>

<p>It is stated here that:</p>

<blockquote>
  <p>The service account flow supports server-to-server interactions that
  do not access user information. However, the YouTube Analytics API
  does not support this flow. Since there is no way to link a Service
  Account to a YouTube account, attempts to authorize requests with this
  flow will generate an error.</p>
</blockquote>

<p>I also found it in this <a href="http://stackoverflow.com/questions/13586153/youtube-analytics-google-service-account">thread</a> answered by a Googler.</p>

<p>For more information, check this <a href="http://stackoverflow.com/questions/15554106/using-the-youtube-analytics-api-with-a-cms-account">SO question</a>.</p>

python youtube-api google-oauth google-api-client