2015-09-07 206 views
0

我正在使用youtube API爲我的應用程序中的用戶安排實時事件。獲取用戶登錄訪問令牌以使用YouTube API

一旦用戶登錄我的應用程序需要在相同的用戶沒有給予登錄憑據登錄到我們的業務谷歌帳戶(一個谷歌帳戶的所有用戶)。並獲取訪問令牌來安排實時事件。

是否有可能登陸的用戶到谷歌帳戶沒有給予登錄憑據(用戶也不會覺得自己是登錄到另一個帳戶)。

PHP是否可行?請舉一個例子來獲取訪問令牌以訪問YouTube。

我使用以下代碼獲取訪問令牌,但服務帳戶無法訪問YouTube服務。

我使用的服務帳戶獲得訪問令牌代碼:

<?php 
    require_once 'Google/autoload.php'; 
    session_start();  

    $client_id = '[email protected]'; 
    $Email_address = '[email protected]'; 
    $key_file_location = 'Youtube API-5czxczxc86.p12';  

    $client = new Google_Client();  
    $client->setApplicationName("Youtube API"); 
    $key = file_get_contents($key_file_location);  

    // seproate additional scopes with a comma 
    $scopes = array('https://www.googleapis.com/auth/sqlservice.admin','https://www.googleapis.com/auth/plus.login','https://www.googleapis.com/auth/youtube'); 

    $cred = new Google_Auth_AssertionCredentials($Email_address,   
          $scopes,  
          $key); 

    $client->setAssertionCredentials($cred); 
    if($client->getAuth()->isAccessTokenExpired()) {   
     $client->getAuth()->refreshTokenWithAssertion($cred);  
    } 
    //print_r($client); 
    echo $client->getAccessToken(); 
    ?> 

我期待的答案是一樣的東西this.Any一個請幫助關於YouTube API

+0

使用'refreshToken'自動獲得新的'access_token' –

+0

$ client-> setAccessType('offline'); // Get us our refreshtoken – DaImTo

+0

如何自動登錄用戶? – Kichu

回答

相關問題