PHP:我可以使用帐户和密码来授权google-api-client连接吗?

时间:2014-04-09 21:38:14

标签: php google-api-php-client

在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。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用service account

  

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

Here's a sample code使用PHP库。