本文介绍了从哪里获得 Google Analytics API access_token?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Google Analytics API 参考 OAuth 以及访问令牌可用于访问页面的统计信息.使用 OAuth 非常复杂,或者至少在我看来就像在 php 中一样.但是根据文档,我也应该能够使用 access_code 作为获取参数.

According to Google Analytics API Reference OAuth as well as an Access Token can be used to access the stats of an page. Using OAuth is pretty complicated or at least it seems to me like that in php. However according to the docs I should also be able to use access_code as an get parameter.

我尝试了多种方法来从 google 开发者控制台获取 access_token,但两种方法都无法始终返回以下错误:

I tried several ways to get the access_token from the google developer console, but neither is working always returning the following error:

{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}

因此,我想知道如何为我的 google 分析页面属性获取不过期的 api 访问代码?如果可能的话?

Thus I'm wondering how to get an none expireing api access code for my google analytics page properties? if possible at all?

推荐答案

您需要在 Google 开发者控制台.它是使用 Oauth2 获取访问令牌的问题.您首先要求用户授予访问其数据的权限,从而获得访问令牌.获得许可后,您将收到一个刷新令牌,可用于获取访问令牌.

You need to register your application on Google Developer console first. Its a matter of using Oauth2 to get an access token. You get an access token by first asking the user for permission to access their data. Once you have been given permission you will then receive a refresh token which you can use to get an access token.

以下示例使用在 GitHub 上找到的 Google PHP 客户端库.该代码是从我的 Google Oauth2 PHP 教程 教程中复制的.

The following example uses the Google PHP client lib found on GitHub. The code was copied from my Google Oauth2 PHP Tutorial the tutorial.

<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("{devkey}");
    $client->setClientId('{clientid}.apps.googleusercontent.com');
    $client->setClientSecret('{clientsecret}');
    $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

    //For loging out.
    if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
       }

    // Step 2: The user accepted your access now you need to exchange it.
    if (isset($_GET['code'])) {

        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

    // Step 1:  The user has not authenticated we give them a link to login
    if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect Me!</a>";
        }

    // Step 3: We have access we can now create our service
    if (isset($_SESSION['token'])) {
        print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
        $client->setAccessToken($_SESSION['token']);
        $service = new Google_Service_Analytics($client);

        // request user accounts
        $accounts = $service->management_accountSummaries->listManagementAccountSummaries();

       foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> \n";
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> \n";

            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> \n";
                }
            }
        }
    } // closes account summaries

    }
 print "<br><br><br>";
 print "Access from google: " . $_SESSION['token'];
?>

这篇关于从哪里获得 Google Analytics API access_token?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:46