我使用了带有重定向url的Google方法
urn:ietf:wg:oauth:2.0:oob但是最近google刚刚贬低了这个方法,现在我了解到他们要求迁移到带有回环的桌面api。
https://developers.google.com/identity/protocols/oauth2/native-app#redirect-uri_loopback我试过了上面的方法,但没有用。
$client = new Google\Client();
$client->setAuthConfig($dir . 'google/vendor/credentials.json');
$socket = new SocketServer('127.0.0.1','0');
$redirect = $socket->server;
//$redirect = 'urn:ietf:wg:oauth:2.0:oob';
// readyonly
$client->setScopes(array('https://www.googleapis.com/auth/youtube.readonly','https://www.googleapis.com/auth/youtube.force-ssl','https://www.googleapis.com/auth/yt-analytics.readonly'));
$client->setRedirectUri($redirect);
$client->setAccessType('offline');
$client->setPrompt('consent');
$client->setIncludeGrantedScopes(true); // incremental auth如果我将它更改为redirect_url为http://localhost,它可以工作,但不像oob :)
请建议它将如何运作,就像以前一样
更新
使用了建议的代码。
$client->setRedirectUri("http://127.0.0.1");
$client->setPrompt('select_account consent');然后得到了这个

发布于 2022-10-28 12:48:16
只需使用"http://127.0.0.1",如果您有一个旧的credentials.json文件,打开它并在重定向uris下。删除urn:ietf:wg:oauth:2.0:oob客户端库还没有更新以忽略它,因此它只需要文件中的第一个重定向uri,这恰好是urn:ietf:wg:oauth:2.0:oob,这会产生一个错误。
$client = new Client();
$client->setApplicationName('Google Drive API PHP Upload quickstart');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig('C:\Development\FreeLance\GoogleSamples\Credentials\credentials.json');
$client->setAccessType('offline');
$client->setRedirectUri("http://127.0.0.1");
$client->setPrompt('select_account consent');更新404错误
造成404错误的原因是您试图在没有运行web服务器的情况下将"http://127.0.0.1"加载到您的计算机上。这将导致404错误显示在任何网页浏览器,因为它不能到达有关的网页。但是,如果您检查brower窗口中的url栏,就会发现它处于状态。
https://127.0.0.1/?state=xxxxxxx&code=xxxxxxxxxxxxxx&scope=googleapis.com/auth/youtube.readonly googlevis.com/auth/youtube.force-ssl googlevis.com/auth/yt-Analytics.readonly
完成授权过程所需的代码就在那里。您只需将其复制并粘贴到代码中即可。
除去oob,这是它唯一的工作方式,除非您生成一个web服务器来托管本地主机。
注意: Python可以通过打开本地web服务器来实现这一点。
creds = flow.run_local_server(port=0)我不确定这是否是php可以做的事情,因为google没有给我们一个这样做的例子。
请参阅#2334
https://stackoverflow.com/questions/74235555
复制相似问题