首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google OOB迁移PHP

Google OOB迁移PHP
EN

Stack Overflow用户
提问于 2022-10-28 12:38:41
回答 1查看 55关注 0票数 1

我使用了带有重定向url的Google方法

代码语言:javascript
复制
urn:ietf:wg:oauth:2.0:oob

但是最近google刚刚贬低了这个方法,现在我了解到他们要求迁移到带有回环的桌面api。

代码语言:javascript
复制
https://developers.google.com/identity/protocols/oauth2/native-app#redirect-uri_loopback

我试过了上面的方法,但没有用。

代码语言:javascript
复制
        $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 :)

请建议它将如何运作,就像以前一样

更新

使用了建议的代码。

代码语言:javascript
复制
    $client->setRedirectUri("http://127.0.0.1");
    $client->setPrompt('select_account consent');

然后得到了这个

EN

回答 1

Stack Overflow用户

发布于 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,这会产生一个错误。

代码语言:javascript
复制
$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服务器来实现这一点。

代码语言:javascript
复制
creds = flow.run_local_server(port=0)

我不确定这是否是php可以做的事情,因为google没有给我们一个这样做的例子。

请参阅#2334

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74235555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档