PHP:我可以使用帐户和密码来授权GoogleAPI客户端连接吗


PHP: Can I use account and password to authorize a google-api-client connection?

在Google Docs api指南中,Oauth2是作为一种授权方法提供的。这是授权码:

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('...');
$client->setClientSecret('...');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
// Request authorization
print "Please visit:'n$authUrl'n'n";
print "Please enter the auth code:'n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

但是,这种授权需要您单击链接并手动进行授权。我想要的是使用电子邮件帐户和密码来授权连接,这可能吗?

顺便说一句,我不想使用Zend Gdata。提前谢谢。

您可以使用服务帐户。

对于这些类型的服务器到服务器的交互,您需要一个服务帐户,该帐户属于您的应用程序的个人最终用户。您的应用程序在上调用Google API代表服务帐户,并且不需要用户同意。(在非服务帐户场景,您的应用程序在代表最终用户,有时需要用户同意。)

下面是一个使用PHP库的示例代码。