title: Client is unauthorized to retrieve access tokens using this method
body: <p>I have a custom dashboard in Ruby on Rails project with data collected from Google Analytics. I user Google Analytics Reporting V4 API and a Service Account to authenticate.</p>
  
  <p>My code works well if I don't impersonate user <code>authorization.sub = 'xxx@mail.com'</code> and If I do it, I get <code>unauthorized_client</code> error but not all the times. Sometimes it works, and sometimes not.</p>
  
  <p>This is my code:</p>
  
  <pre><code>scope = [Google::Apis::AnalyticsreportingV4::AUTH_ANALYTICS_READONLY]
  
  view_id = 'xxxxxx'
  
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "#{Rails.root}/private/google_analytics_key.json"
  authorization = Google::Auth::get_application_default(scope)
  authorization.sub = 'xxx@mail.com'
  
  analytics = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
  analytics.authorization = authorization
  
  ...
  
  request = Google::Apis::AnalyticsreportingV4::GetReportsRequest.new(report_requests: report_requests)
  
  @result = analytics.batch_get_reports(request)
  </code></pre>
  
  <p>Rails server log output:</p>
  
  <pre><code>Sending HTTP post https://analyticsreporting.googleapis.com/v4/reports:batchGet?
  Caught error Authorization failed.  Server message:
  {
   "error": "unauthorized_client",
   "error_description": "Client is unauthorized to retrieve access tokens using this method."
  }
  Error - #&lt;Signet::AuthorizationError: Authorization failed.  Server message:
  {
   "error": "unauthorized_client",
   "error_description": "Client is unauthorized to retrieve access tokens using this method."
  }&gt;
  
  Completed 500 Internal Server Error in 149ms (ActiveRecord: 0.4ms)
  
  
  
  Signet::AuthorizationError (Authorization failed.  Server message:
  {
   "error": "unauthorized_client",
   "error_description": "Client is unauthorized to retrieve access tokens using this method."
  }):
  </code></pre>