【问题标题】:“Google MC Funnels API” (401 Unauthorized)“Google MC Funnels API”(401 未授权)
【发布时间】:2015-12-01 00:40:12
【问题描述】:

我设置了一个 MC Funnels 示例,一切正常!!:

  • 从 git 下载 Master。
  • 输入一个帐户即可使用!
  • 没关系!向我返回一些书籍样本的结果...

所以,我输入新代码来获取“mcf:”但收到“401 Unauthorized

问题:如何/在何处授权对“mcf:xxxxxxx”指标和维度的查询?

我使用的代码:

include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
// $client->setApplicationName("Client_Library_Examples");
// $client->setScopes(['https://www.googleapis.com/auth/books']);    //not sure here..

// $service = new Google_Service_Books($client);
   $service = new Google_Service_Analytics($client);      //not sure here..

        $mcf = $service->data_mcf(
            'ga:80456636',
            '2015-02-22',
            '2015-02-23',
            'mcf:totalConversionValue',
            array(
                'dimensions' => 'mcf:source,mcf:medium'
            )
        );

        echo"<pre>";
        var_dump($mcf);
        echo"</pre>";

【问题讨论】:

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


    【解决方案1】:

    首先,您注释掉了 $client 的所有代码。 Google 图书是一个公共 API,这意味着您可以使用公共 API 密钥进行身份验证。 Google Analytics 数据是私人数据,这意味着您需要使用 Oauth2 或服务帐户。

    服务帐号示例

       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);
    
    $mcf = $service->data_mcf->get('ga:80456636',
                '2015-02-22',
                '2015-02-23',
                'mcf:totalConversionValue',
                array(
                    'dimensions' => 'mcf:source,mcf:medium'
                )
            );
    
            echo"<pre>";
            var_dump($mcf);
            echo"</pre>";
    

    【讨论】:

    • 是的,使用您从 Google 开发者控制台获得的信息。
    • 缺少一个类:**致命错误:在 ** 中找不到类“Google_Auth_AssertionCredentials”?请问这是什么?
    • 您没有下载部分客户端库可能我的示例使用 v1 主分支 github.com/google/google-api-php-client/tree/v1-master 而不是作曲家
    • 好的!正确的版本有效!谢谢! 但另一个错误: -> `致命错误:调用未定义的方法 Google_Service_Analytics::data_mcf() in....'
    • 你真的应该看看客户端库代码,你可以自己解决这个问题github.com/google/google-api-php-client/blob/master/src/Google/… $service->data_mcf->get()
    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 2016-03-03
    • 2011-08-02
    • 1970-01-01
    • 2014-10-22
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多