我们目前正在编写一个分析脚本,从Google和YouTube Analytics中提取信息。我们目前正在使用oAUTH身份验证。所有的账户都由谷歌的一个中央账户连接在一起。YouTube帐户有多个通道连接到它。当使用oauth对YouTube分析进行身份验证时,它要求您选择主帐户(它链接到Google并从另一个帐户委托访问),或者二级YouTube通道帐户。如果您进入帐户,那么YouTube分析就不能工作,反之亦然。有人对如何让每个API与同一个登录令牌下的单独帐户关联有什么建议吗?
发布于 2014-03-26 18:29:06
此问题的解决方案是创建两个独立的脱机键。在没有数据库连接的情况下,我已经这样做了(但是如果您替换了这段代码的一部分,可以很容易地添加一个数据库连接)。请注意,我只是在学习PHP,所以请说明代码问题/缩进问题。
为了使其正常工作,Google必须在两个单独的文件中创建两个访问令牌。
下面是YouTube分析生成器(get_yt_access_token.php)的示例:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeAnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('YT Analytics App');
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/get_yt_access_token.php');
$client->setScopes(array(
'https://www.googleapis.com/auth/yt-analytics.readonly',
'https://www.googleapis.com/auth/youtube.readonly'
));
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$myFile = "refreshyttoken.conf";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $client->getAccessToken());
fclose($fh);
} else {
if (!$client->getAccessToken()) {
$auth = $client->createAuthUrl();
header("Location: $auth");
}
}注意,refreshmytoken.conf是正在生成的文件的名称。
下面是Google生成器(get_ga_access_token.php)的示例:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('GA Analytics App');
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/get_access_token.php');
$client->setScopes('https://www.googleapis.com/auth/analytics.readonly');
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$myFile = "refreshgatoken.conf";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $client->getAccessToken());
fclose($fh);
} else {
if (!$client->getAccessToken()) {
$auth = $client->createAuthUrl();
header("Location: $auth");
}
}注意,refreshgatoken.conf是正在生成的文件的名称。
Google对Google的要求:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeAnalyticsService.php';在这些函数上,请记住setClientID、RedirectUri和适用于您的服务器和Google密钥的内容。另外,ViewID和ChannelID也被故意排除在外。
例如Google分析功能:
$viewId = "ga:xxxxxxx";
function OutboundClicksweek(){
$client = new Google_Client();
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/analytics.php');
$client->setScopes('https://www.googleapis.com/auth/analytics.readonly');
$client->setAccessToken(file_get_contents('refreshgatoken.conf'));
$client->setUseObjects(true);
$service = new Google_AnalyticsService($client);
$start_date = $GLOBALS["start_date"];
$end_date = new DateTime($start_date);
$end_date->add(new DateInterval('P6D'));
$end_date = $end_date->format('Y-m-d');
$metrics = "ga:totalEvents";
$dimensions = "ga:eventCategory";
$filters = "ga:eventCategory=~Outbound Traffic";
$optParams = array('dimensions' => $dimensions, 'filters' => $filters);
$props = $GLOBALS["service"]->data_ga->get($GLOBALS["viewId"],$start_date,$end_date,$metrics,$optParams);
$events=$props->totalsForAllResults['ga:totalEvents'];
return $events;
}; YouTube函数示例:
$YTChannelID = "channel==xxxxxxxxx";
function YoutubeFacebookAnalytics(){
$client = new Google_Client();
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/analytics.php');
$client->setScopes(array('https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/youtube.readonly'));
$client->setAccessToken(file_get_contents('refreshyttoken.conf'));
$client->setUseObjects(true);
$service = new Google_YouTubeAnalyticsService($client);
$start_date = $GLOBALS["start_date"];
$end_date = new DateTime($start_date);
$end_date->add(new DateInterval('P6D'));
$end_date = $end_date->format('Y-m-d');
$metrics = "views";
$dimensions = "insightTrafficSourceDetail";
$filters = "insightTrafficSourceType==EXT_URL";
$max_results = "25";
$sort = "-views";
$optParams = array('dimensions' => $dimensions, 'filters' => $filters, 'max-results' => $max_results, 'sort' => $sort);
$pages = $service->reports->query($GLOBALS['YTChannelID'], $start_date, $end_date, $metrics, $optParams);
print_r($pages);
};一旦编码完成,只需转到浏览器中的get访问令牌php文件并登录到适当的Google帐户。然后回到你原来的页面,你应该会做得很好。
https://stackoverflow.com/questions/22646153
复制相似问题