FBRC
FBRC

Reputation: 173

Google Analytics reporting api via curl

I'm getting the token via oauth consent, but the code below returns 401 error. Should the access token go in the header as 'Authorization'?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://analyticsreporting.googleapis.com/v4/reports:batchGet");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: ya29.A0ARrdaM-otJeAg4muGQIIi...............'
));
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(ARRAY_DATA));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_getinfo($ch);
$result = curl_exec($ch);
curl_close($ch);

Response:

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CREDENTIALS_MISSING",
        "domain": "googleapis.com",
        "metadata": {
          "method": "google.analytics.reporting.v4.Reporting.GetReports",
          "service": "analyticsreporting.googleapis.com"
        }
      }
    ]
  }
}

Upvotes: 1

Views: 1973

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

You should send the access token to the Google analytics reporting api as a Authorization header bearer token.

Yes but you are missing the fact that its Bearer token

'Authorization: Bearer ya29.A0ARrdaM-otJeAg4muGQIIi...............'

Upvotes: 1

Related Questions