小编jpk*_*jpk的帖子

如何在PHP中获取谷歌分析实时activeUsers?

我从https://github.com/google/google-api-php-client下载了API文件, 并尝试使用以下代码...

$client->setApplicationName("Analytics");
$client->setDeveloperKey("key");
$service = new Google_Service_Analytics($client);

$optParams = array(
    'dimensions' => 'rt:medium');

try {
  $results = $service->data_realtime->get(
      'ga:profileid',
      'rt:activeUsers',
      $optParams);
  // Success.
} catch (apiServiceException $e) {
  // Handle API service exceptions.
  $error = $e->getMessage();
}
print_r($results);
Run Code Online (Sandbox Code Playgroud)

它显示错误(401)需要登录'

当我通过https://console.developers.google.com/project/apps~rational-photon-578/apiui/api/analytics/method/analytics.data.realtime.get尝试通话时

我得到了所需的实时用户所属的profileid.

那么我该如何实现登录过程(无需访问谷歌网站)呢?

我尝试了以下代码......

$client_id              = 'xxxxxxxxxxxxxxx';
$service_account_name   = 'xxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
$key_file_location      = 'location of .p12 keyfile';

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

if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}

$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/analytics'),
$key …
Run Code Online (Sandbox Code Playgroud)

php google-api google-analytics-api oauth-2.0 google-oauth

6
推荐指数
1
解决办法
3979
查看次数

获取错误Socket正在尝试重新连接到Sails

My Sails版本为0.11.2,运行端口为1337

在assets/js/dependencies/sails.io.js中,我可以看到版本为0.11.0

下面是客户端脚本.

<script src="http://localhost/project/assets/js/dependencies/sails.io.js"></script>
<script type="text/javascript">

// `io` is available as a global.
// `io.socket` will connect automatically, but at this point in the DOM, it is not ready yet
// (think of $(document).ready() from jQuery)
//
// Fortunately, this library provides an abstraction to avoid this issue.
// Requests you make before `io` is ready will be queued and replayed automatically when the socket connects.
// To disable this behavior or configure other things, you can set …
Run Code Online (Sandbox Code Playgroud)

javascript node.js socket.io sails.js

0
推荐指数
1
解决办法
1551
查看次数