使用YouTube API登录,并保存信息以供以后使用,例如为我的客户上传Vidoes等



以下是代码样本。但是,我无法为已登录的用户保存信息,然后系统管理员使用API上传视频。

现在,当前每个用户都必须从Google登录和身份验证以获取访问令牌以上传Vidoes等。我想要一种注册的方法,然后我可以在任何时候使用信息,并且用户不需要再次进行身份验证从Google获取会话数据/令牌。

$ client-> $ client-> getAccessToken()令牌给我

{" access_token":" xxxxxxxxx"," token_type":" bearer"," expires_in":3600, " refresh_token":" xxxxxxx","创建":000000}

我想存储/保存一些信息,如果用户登录或关闭用户,则可以在以后使用其他人使用。

 <?php
    // Call set_include_path() as needed to point to your client library.
    set_include_path($_SERVER['DOCUMENT_ROOT'] . '/directory/to/google/api/');
    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 Cloud Console }} <{{ https://cloud.google.com/console }}>
     * 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 = 'XXXXXXX.apps.googleusercontent.com';
    $OAUTH2_CLIENT_SECRET = 'XXXXXXXXXX';
    $REDIRECT = 'http://localhost/oauth2callback.php';
    $APPNAME = "XXXXXXXXX";

    $client = new Google_Client();
    $client->setClientId($OAUTH2_CLIENT_ID);
    $client->setClientSecret($OAUTH2_CLIENT_SECRET);
    $client->setScopes('https://www.googleapis.com/auth/youtube');
    $client->setRedirectUri($REDIRECT);
    $client->setApplicationName($APPNAME);
    $client->setAccessType('offline');

    // 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();
    }
    if (isset($_SESSION['token'])) {
        $client->setAccessToken($_SESSION['token']);
        echo '<code>' . $_SESSION['token'] . '</code>';
    }
    // Check to ensure that the access token was successfully acquired.
    if ($client->getAccessToken()) {
        try {
            // Call the channels.list method to retrieve information about the
            // currently authenticated user's channel.
            $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
                'mine' => 'true',
            ));
            $htmlBody = '';
            foreach ($channelsResponse['items'] as $channel) {
                // Extract the unique playlist ID that identifies the list of videos
                // uploaded to the channel, and then call the playlistItems.list method
                // to retrieve that list.
                $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];
                $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
                    'playlistId' => $uploadsListId,
                    'maxResults' => 50
                ));
                $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>";
                foreach ($playlistItemsResponse['items'] as $playlistItem) {
                    $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
                        $playlistItem['snippet']['resourceId']['videoId']);
                }
                $htmlBody .= '</ul>';
            }
        } catch (Google_ServiceException $e) {
            $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
                htmlspecialchars($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 {
        $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">authorise access</a> before proceeding.<p>
    END;
    }
    ?>
    <!doctype html>
    <html>
    <head>
        <title>My Uploads</title>
    </head>
    <body>
    <?php echo $htmlBody?>
    </body>
    </html>

要实现此目标,每当您的应用程序登录新的用户时,将refresh Token保存在某些数据库中。现在,每次已经经过身份验证的用户登录时,都会查找具有相同刷新令牌的用户。如果匹配,请使用该refresh token为用户生成访问令牌。刷新代币永远不会到期,因此您可以依靠它们来验证已经注册的用户。希望这对您有帮助!从文档中查看此语句

将刷新令牌保存在安全的长期存储中,并继续使用它们,只要它们保持有效。限制适用于每个客户端使用者组合发出的刷新令牌的数量,并且所有客户端的用户都不同,这些限制是不同的。如果您的申请要求足够的刷新令牌限制其中一个限制,则较旧的刷新令牌停止工作。

最新更新