ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Google PHP客户端库 – “需要登录”例外

2019-07-02 19:15:40  阅读:246  来源: 互联网

标签:php google-api google-oauth google-analytics-api google-api-php-client


尝试访问我的Google Analytics数据时收到以下错误:异常’Google_Service_Exception’,显示消息’错误调用GET …我的查询…’:( 401)需要登录

我不知道如何解决这个问题,而且我已经花了好几个小时试图设置它并没有成功.

这是我的代码:

        $client = new \Google_Client();
        $client->setApplicationName("My App");
        $client->setDeveloperKey('my API key');

        $analytics = new \Google_Service_Analytics($client);

        $OBJresult = $analytics->data_ga->get(
            'ga:myprofileid' .,
            '2012-01-01',
            date("Y-m-d"),
            'ga:visits',
            array(
                'filters' => 'ga:pagePath==/home',
                'dimensions' => 'ga:pagePath',
                'metrics' => 'ga:pageviews',
                'sort' => '-ga:pageviews'
            )
        );

解决方法:

如果您只访问自己的数据,那么您应该使用服务帐户.如果您希望能够登录并查看其他人的数据,那么您应该使用Oauth2.

服务帐户示例:

<?php
   require_once 'Google/autoload.php';
   session_start();     
/************************************************   
 The following 3 values an befound in the setting   
 for the application you created on Google      
 Developers console.         Developers console.
 The Key file should be placed in a location     
 that is not accessable from the web. outside of 
 web root.       web root.

 In order to access your GA account you must    
 Add the Email address as a user at the     
 ACCOUNT Level in the GA admin.         
 ************************************************/
    $client_id = '[Your client id]';
    $Email_address = '[YOur Service account email address Address]';     
    $key_file_location = '[Locatkon of key file]';      

    $client = new Google_Client();      
    $client->setApplicationName("Client_Library_Examples");
    $key = file_get_contents($key_file_location);    

    // seproate additional scopes with a comma   
    $scopes ="https://www.googleapis.com/auth/analytics.readonly";  

    $cred = new Google_Auth_AssertionCredentials($Email_address,         
                             array($scopes),        
                             $key);     

    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {        
         $client->getAuth()->refreshTokenWithAssertion($cred);      
    }       

    $service = new Google_Service_Analytics($client);



    //Adding Dimensions
    $params = array('dimensions' => 'ga:userType'); 
    // requesting the data  
    $data = $service->data_ga->get("ga:89798036", "2014-12-14", "2014-12-14", "ga:users,ga:sessions", $params );     
?>

<html>   
Results for date:  2014-12-14<br>
    <table border="1">   
        <tr>     
        <?php    
        //Printing column headers
        foreach($data->getColumnHeaders() as $header){
             print "<td><b>".$header['name']."</b></td>";       
            }       
        ?>      
        </tr>       
        <?php       
        //printing each row.
        foreach ($data->getRows() as $row) {        
            print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";   
        }    
?>      
<tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr>     
</table>     
</html>     

有用的网址:

>代码从Service account tutorial撕掉
> Google Analytics oauth2 tutorial
>谷歌的新官方教程Hello Analytics php

标签:php,google-api,google-oauth,google-analytics-api,google-api-php-client
来源: https://codeday.me/bug/20190702/1359158.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有