Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Google帐户自动登录并使用php获取访问令牌_Php_Google Api_Youtube Api_Google Api Php Client - Fatal编程技术网

Google帐户自动登录并使用php获取访问令牌

Google帐户自动登录并使用php获取访问令牌,php,google-api,youtube-api,google-api-php-client,Php,Google Api,Youtube Api,Google Api Php Client,我正在我的应用程序中使用YouTube API进行实时流媒体。为了安排直播,我将为每个用户提供一个通用的电子邮件id和密码 我需要使用通用的电子邮件id和密码自动将用户登录到google帐户(不提供电子邮件id和密码),并使用php获取访问令牌 可能吗 我的当前代码如下。请建议一种解决方案 <?php // Call set_include_path() as needed to point to your client library. require_once 'Google/aut

我正在我的应用程序中使用YouTube API进行实时流媒体。为了安排直播,我将为每个用户提供一个通用的电子邮件id和密码

我需要使用通用的电子邮件id和密码自动将用户登录到google帐户(不提供电子邮件id和密码),并使用php获取访问令牌

可能吗

我的当前代码如下。请建议一种解决方案

<?php

// Call set_include_path() as needed to point to your client library.
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID = '$OAUTH2_CLIENT_ID';
$OAUTH2_CLIENT_SECRET = '$OAUTH2_CLIENT_SECRET';
$htmlBody="";
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
if(isset($_POST['title']) && isset($_POST['timeStart']) && isset($_POST['timeEnd']) && isset($_POST['Status']))
{
  try {
    // Create an object for the liveBroadcast resource's snippet. Specify values
    // for the snippet's title, scheduled start time, and scheduled end time.
    $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
    $broadcastSnippet->setTitle($_POST['title']);
    $broadcastSnippet->setScheduledStartTime($_POST['timeStart']);
    $broadcastSnippet->setScheduledEndTime($_POST['timeEnd']);

    // Create an object for the liveBroadcast resource's status, and set the
    // broadcast's status to "private".
    $status = new Google_Service_YouTube_LiveBroadcastStatus();
    $status->setPrivacyStatus($_POST['Status']);

    // Create the API request that inserts the liveBroadcast resource.
    $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
    $broadcastInsert->setSnippet($broadcastSnippet);
    $broadcastInsert->setStatus($status);
    $broadcastInsert->setKind('youtube#liveBroadcast');

    // Execute the request and return an object that contains information
    // about the new broadcast.
    $broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status',
        $broadcastInsert, array());

    // Create an object for the liveStream resource's snippet. Specify a value
    // for the snippet's title.
    $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
    $streamSnippet->setTitle('New Stream');

    // Create an object for content distribution network details for the live
    // stream and specify the stream's format and ingestion type.
    $cdn = new Google_Service_YouTube_CdnSettings();
    $cdn->setFormat("1080p");
    $cdn->setIngestionType('rtmp');

    // Create the API request that inserts the liveStream resource.
    $streamInsert = new Google_Service_YouTube_LiveStream();
    $streamInsert->setSnippet($streamSnippet);
    $streamInsert->setCdn($cdn);
    $streamInsert->setKind('youtube#liveStream');

    // Execute the request and return an object that contains information
    // about the new stream.
    $streamsResponse = $youtube->liveStreams->insert('snippet,cdn',
        $streamInsert, array());

    // Bind the broadcast to the live stream.
    $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
        $broadcastsResponse['id'],'id,contentDetails',
        array(
            'streamId' => $streamsResponse['id'],
        ));

    $htmlBody .= "<h3>Added Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
        $broadcastsResponse['snippet']['title'],
        $broadcastsResponse['snippet']['publishedAt'],
        $broadcastsResponse['id']);
    $htmlBody .= '</ul>';
    $htmlBody .= "<h3>Added Stream</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $streamsResponse['snippet']['title'],
        $streamsResponse['id']);
    $htmlBody .= '</ul>';
    $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
        $bindBroadcastResponse['id'],
        $bindBroadcastResponse['contentDetails']['boundStreamId']);
    $htmlBody .= '</ul>';
    //$htmlBody .="<h3>Live  Broadcast</h3><div>";

    //$htmlBody .= sprintf("<iframe id='ytplayer' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/%s?autoplay=1'  frameborder='0'></iframe>",
        //  $broadcastsResponse['id']);
    $htmlBody .= '</div>';


  } catch (Google_Service_Exception $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
      // echo $e->getMessage();
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }

  $_SESSION['token'] = $client->getAccessToken();
  } else {
  if(isset($_GET['propid']))
  {
  $htmlBody = <<<END
  <form action="" method="post">
<input type="text" name="title" value="Event Title"/><br/>
<input type="text" name="timeStart" value="Start Time"/><br/>
<input type="text" name="timeEnd" value="End Time"/><br/>
<input type="text" name="Status" value="Public,Unlisted,Private"/>
<input type ="submit" value="Submit"/>
</form>
END;
} else {

 $htmlBody = <<<END
 <p>Close this window and click the schedule button for live streaming</p>
<p>You need to <a href="#"  onclick="window.close();">Close Window</a> before proceeding.<p>
END;

}

}
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl" onclick="window.open('$authUrl', 'newwindow', 'width=600, height=400'); return false;">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>Bound Live Broadcast</title>
</head>
<body>
  <?=$htmlBody?>
</body>
</html>

使用登录名和密码登录API称为客户端登录

ClientLogin从年起被正式弃用,现在不再可用。对ClientLogin的请求将以HTTP 404响应失败


答:不,不可能使用用户登录名和密码以编程方式访问YouTube API。通过YouTube API访问用户拥有的私有数据的唯一方法是使用Oauth2

使用登录名和密码登录API称为客户端登录

ClientLogin从年起被正式弃用,现在不再可用。对ClientLogin的请求将以HTTP 404响应失败


答:不,不可能使用用户登录名和密码以编程方式访问YouTube API。通过YouTube API访问用户拥有的私有数据的唯一方法是使用Oauth2

我只有一个用户帐户供所有用户安排实时流。该计划仅在用户登录后可用。用户无需为schedule live stream提供google帐户登录。可以通过php实现吗?可以,但我没有一个有效的示例。使用$client->setAccessType('offline')对代码进行身份验证;要获取刷新令牌,请保存刷新令牌。然后使用refreshtoken获取访问令牌,无需再次注销。我只有一个用户帐户供所有用户安排实时流。该计划仅在用户登录后可用。用户无需为schedule live stream提供google帐户登录。可以通过php实现吗?可以,但我没有一个有效的示例。使用$client->setAccessType('offline')对代码进行身份验证;要获取刷新令牌,请保存刷新令牌。然后使用refreshtoken获取访问令牌,而无需再次注销。