Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 第三方Youtube API视频上传卡在授权访问上_Php_Google Api_Google Oauth_Youtube Data Api_Google Api Php Client - Fatal编程技术网

Php 第三方Youtube API视频上传卡在授权访问上

Php 第三方Youtube API视频上传卡在授权访问上,php,google-api,google-oauth,youtube-data-api,google-api-php-client,Php,Google Api,Google Oauth,Youtube Data Api,Google Api Php Client,我正在尝试通过一个PHP脚本上传视频,使用YouTube API V3和一个免费的第三方脚本。我已经看过多个脚本,包括YouTube代码片段,但这一个似乎最适合我的需要 我现在遇到的问题是,脚本似乎陷入了一个授予访问权限、重新开始并再次请求访问权限的循环,而从未上传过视频 此外,这个脚本专门用于只授予一次访问权限,这正是我所需要的。 我已经在第三方网站上寻求帮助,遗憾的是还没有得到回应 我们已经配置了应用程序,获得了正确的凭据,设置了redirct URI等等,显然是正确的,因为我们确实得到了允

我正在尝试通过一个PHP脚本上传视频,使用YouTube API V3和一个免费的第三方脚本。我已经看过多个脚本,包括YouTube代码片段,但这一个似乎最适合我的需要

我现在遇到的问题是,脚本似乎陷入了一个授予访问权限、重新开始并再次请求访问权限的循环,而从未上传过视频

此外,这个脚本专门用于只授予一次访问权限,这正是我所需要的。 我已经在第三方网站上寻求帮助,遗憾的是还没有得到回应

我们已经配置了应用程序,获得了正确的凭据,设置了redirct URI等等,显然是正确的,因为我们确实得到了允许我们授权应用程序使用YouTube帐户/频道的响应。 脚本似乎无法通过API方法获取accessToken

原始代码可在以下位置找到:

失败的原因似乎是:

$client->getAccessToken();
存在if-else构造,脚本始终使用else部分

if($client->getAccessToken()){…}
否则{
//如果用户尚未授权应用程序,则启动OAuth流
$state=mt_rand();
$client->setState($state);
$\会话['state']=$state;
$authUrl=$client->createAuthUrl();

$htmlBody=这是我用于授权的代码


对于上传,我将遵循

这是我用于授权的代码

对于上传,我会跟随

require_once __DIR__ . '/vendor/autoload.php';
/**
 * Gets the Google client refreshing auth if needed.
 * Documentation: https://developers.google.com/identity/protocols/OAuth2
 * Initializes a client object.
 * @return A google client object.
 */
function getGoogleClient() {
    $client = getOauth2Client();
    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }
return $client;
}
/**
 * Builds the Google client object.
 * Documentation: https://developers.google.com/identity/protocols/OAuth2
 * Scopes will need to be changed depending upon the API's being accessed.
 * Example:  array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
 * List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
 * @return A google client object.
 */
function buildClient(){

    $client = new Google_Client();
    $client->setAccessType("offline");        // offline access.  Will result in a refresh token
    $client->setIncludeGrantedScopes(true);   // incremental auth
    $client->setAuthConfig(__DIR__ . '/client_secrets.json');
    $client->addScope([YOUR SCOPES HERE]);
    $client->setRedirectUri(getRedirectUri());  
    return $client;
}
/**
 * Builds the redirect uri.
 * Documentation: https://developers.google.com/api-client-library/python/auth/installed-app#choosingredirecturi
 * Hostname and current server path are needed to redirect to oauth2callback.php
 * @return A redirect uri.
 */
function getRedirectUri(){
    //Building Redirect URI
    $url = $_SERVER['REQUEST_URI'];                    //returns the current URL
    if(strrpos($url, '?') > 0)
        $url = substr($url, 0, strrpos($url, '?') );  // Removing any parameters.
    $folder = substr($url, 0, strrpos($url, '/') );   // Removeing current file.
    return (isset($_SERVER['HTTPS']) ? "https" : "http") . '://' . $_SERVER['HTTP_HOST'] . $folder. '/oauth2callback.php';
}
/**
 * Authenticating to Google using Oauth2
 * Documentation:  https://developers.google.com/identity/protocols/OAuth2
 * Returns a Google client with refresh token and access tokens set. 
 *  If not authencated then we will redirect to request authencation.
 * @return A google client object.
 */
function getOauth2Client() {
    try {

        $client = buildClient();

        // Set the refresh token on the client. 
        if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
            $client->refreshToken($_SESSION['refresh_token']);
        }

        // If the user has already authorized this app then get an access token
        // else redirect to ask the user to authorize access to Google Analytics.
        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

            // Set the access token on the client.
            $client->setAccessToken($_SESSION['access_token']);                 

            // Refresh the access token if it's expired.
            if ($client->isAccessTokenExpired()) {              
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
                $client->setAccessToken($client->getAccessToken()); 
                $_SESSION['access_token'] = $client->getAccessToken();              
            }           
            return $client; 
        } else {
            // We do not have access request access.
            header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
        }
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Oauth2Authentication.php';
// Start a session to persist credentials.
session_start();
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
    $client = buildClient();
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
    $client = buildClient();
    $client->authenticate($_GET['code']); // Exchange the authencation code for a refresh token and access token.
    // Add access token and refresh token to seession.
    $_SESSION['access_token'] = $client->getAccessToken();
    $_SESSION['refresh_token'] = $client->getRefreshToken();    
    //Redirect back to main script
    $redirect_uri = str_replace("oauth2callback.php",$_SESSION['mainScript'],$client->getRedirectUri());    
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}