我们目前正在编写一个分析脚本,用于从Google Analytics和YouTube Analytics中提取信息。我们目前正在使用oAUTH认证。所有帐户都由一个中央Google帐户绑定在一起。 YouTube帐户拥有多个频道。使用oauth对YouTube Analytics进行身份验证时,会要求您选择主帐户(链接到Google Analytics和从另一个帐户委派的访问权限)或辅助YouTube频道帐户。如果您使用Google Analytics(分析)帐户,则YouTube Analytics无法使用,反之亦然。有没有人有任何建议如何告诉每个API与同一登录令牌下的单独帐户关联?YouTube Analytics API在单个用户下登录多个帐户
2
A
回答
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帐户。然后回到你的原始页面,你应该很好去。
相关问题
- 1. 一个market.android.com/publish帐户多次登录
- 2. 多个登录帐户的会话
- 3. 多个帐户同会话登录
- 4. Django多个并发登录帐户
- 5. Azure AD单点登录个人帐户
- 6. 在一个Google Analytics(分析)帐户下跟踪多个域名
- 7. Android多个用户登录Facebook api 3
- 8. 在同一根帐户下管理多个Google Analytics(分析)帐户?
- 9. YouTube API - 使用Google管理员帐户访问多个YouTube频道(品牌帐户)
- 10. collection.googleanalytics'找不到当前登录用户的Google Analytics帐户。'
- 11. Twitter单帐户登录iPhone
- 12. 谷歌API检测多个帐户登录
- 13. YouTube API:“未关联YouTube帐户”与YouTube商家帐户
- 14. Json登录多个用户登录在一个登录页面
- 15. YouTube API - 用户帐户关闭
- 16. 登录多个用户
- 17. ASP.net多个用户登录
- 18. 登录多个用户
- 19. 哪一个是最佳实践:系统中所有登录用户或多个用户帐户的root帐户
- 20. 如何在YouTube API中获取与Google帐户关联的多个用户名?
- 21. 无法登录Google帐户用户API无法升级YouTube会话令牌
- 22. Zend Youtube API - 在单个帐户上上传视频?
- 23. MVC 5 c# - 为每个用户帐户创建一个网站,多个登录
- 24. 登录用户从多个IdentityServer客户
- 25. Youtube Analytics和Google服务帐户
- 26. 阻止2个用户登录到同一个帐户
- 27. 在同一用户帐户下设置多个mercurial用户
- 28. 使用一个Google Analytics帐户管理多个应用程序?
- 29. 多用户登录表单
- 30. 使用Google帐户登录