dql123000 2017-02-24 14:02
浏览 40
已采纳

Google Analytics报告Api - PHP:添加用户帐户

What I want to do

  • Let user of my web application share their analytics account with the app.
  • Redirect the user to Google authentication which ask them to approve access
  • Redirect the user back to the app.
  • Generate a report for the user based on his analytics data and data from the app.
  • Next time the user will generate their report I don't want to ask them for the logging again.

Where the problem is

I read this Google developers PHP guide. However, the sample code connects to first account that is already shared with the app.

I want to see a piece of code that ask user for their credentials and consequently provides access to their account.

On Stack I've already found this answer Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?. However, mentioned APP_EMAIL is not a particular user's e-mail, but developer.gserviceaccount.com identification for the app created in the console.

Also I found this thread Google Analytics Core Reporting API Version 3.0 without client login. It seems that similar topic is solved there, but the important part of code (from my point of view) is missing there.

  • 写回答

1条回答 默认 最新

  • douyi9787 2017-02-24 14:21
    关注

    First of all, make sure to grab refresh token by settings access type to offline. Then Create a separate page for reporting. Separate page should be using account id and refresh token for all reporting, and if it gives you an error on token, you can either refresh it yourself, or send them back to login page.

    you only need to ask for credentials once and you get access to all accounts. The php guide only uses the first account, however you can iterate through all of them and get more

    $client = new Google_Client();
    $client->setAuthConfig(__DIR__ . '/creds/client_secrets.json');
    $client->setIncludeGrantedScopes(true);
    $client->setAccessType("offline");
    $client->setApprovalPrompt('force');
    $client->addScope(Google_Service_Analytics::ANALYTICS_EDIT);
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
    
    // If the user has already authorized this app then get an access token
    // else redirect to ask the user to authorize access to Google Analytics.
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
            // Set the access token on the client.
            $client->setAccessToken($_SESSION['access_token']);
            // Create an authorized analytics service object.
            $analytics = new Google_Service_Analytics($client);
            $accounts=getAccountIds($analytics);
            foreach($accounts as $acc)
            {
             var_dump($acc);
    }
    }
    else {
            $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
            header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么