2014-03-25 62 views
2

我们目前正在编写一个分析脚本,用于从Google Analytics和YouTube Analytics中提取信息。我们目前正在使用oAUTH认证。所有帐户都由一个中央Google帐户绑定在一起。 YouTube帐户拥有多个频道。使用oauth对YouTube Analytics进行身份验证时,会要求您选择主帐户(链接到Google Analytics和从另一个帐户委派的访问权限)或辅助YouTube频道帐户。如果您使用Google Analytics(分析)帐户,则YouTube Analytics无法使用,反之亦然。有没有人有任何建议如何告诉每个API与同一登录令牌下的单独帐户关联?YouTube Analytics API在单个用户下登录多个帐户

回答

1

此问题的解决方案是创建两个独立的脱机密钥。我没有进行数据库连接就完成了这个任务(但是如果您替换了部分代码,可以轻松地添加数据库连接)。请注意,我只是学习PHP,所以请裸露代码问题/缩进问题。

为了这个工作谷歌必须在两个单独的文件中创建两个访问令牌。

这里是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是所生成的文件的名称。

这里是谷歌分析生成器(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是所生成的文件的名称。

谷歌要求对谷歌API:

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什么是适用于您的服务器和谷歌API密钥。 ViewID和ChannelID也被故意排除。

例谷歌Analytics(分析)功能:

$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帐户。然后回到你的原始页面,你应该很好去。

相关问题