【问题标题】:Google OAuth in PHP without using webviewPHP 中的 Google OAuth 不使用 webview
【发布时间】:2018-01-29 16:00:53
【问题描述】:

我最近开发了一个允许用户通过谷歌登录的应用程序。我刚刚注意到(在测试中)谷歌不允许在设备 web 视图中访问 OAuth。

我目前的google登录代码如下

    $client = new \Google_Client();
    $url = 'myurl';
    $client->setAuthConfigFile('../myconfigfile.com.json');
    $client->setRedirectUri($url);
    $client->addScope(\Google_Service_Drive::DRIVE_METADATA_READONLY);
    if (!isset($_GET['code'])) {
        /* Google App Client Id */
        define('CLIENT_ID', 'myid');
        /* Google App Client Secret */
        define('CLIENT_SECRET', 'mysecret');
        /* Google App Redirect Url */
        define('CLIENT_REDIRECT_URL', $url);
        $googleURL = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online';
        header("Location: " .$googleURL);
        exit();
    }

此代码在浏览器上运行良好,但是当我在设备上运行它(目前在 iPad 上测试)时,它会出错并抛出“不允许的用户代理”问题。

我如何获得它以便它加载到浏览器应用程序中,而不是 web 视图中,或者如果这不可行,是否有使用 php 的解决方法?

【问题讨论】:

    标签: php webview user-agent google-oauth


    【解决方案1】:

    您需要通过标头重定向验证服务器端,而不是客户端...

    见:Using OAuth 2.0 for Web Server Applications。

    虽然一个人实际上可以在iOS webview上登录客户端,当添加任一时

    <preference name="OverrideUserAgent" value="Mozilla/5.0 Google"/>
    

    进入config.xml - 或者在使用 JavaScript 覆盖用户代理字符串时:

    webview.getSettings().setUserAgentString("Chrome/56.0.0.0 Mobile");
    

    【讨论】:

    • 可能是一个愚蠢的问题,但是当我需要客户端功能让用户选择他们的帐户时,如何验证服务器端?我的应用程序需要用户能够使用自己的 google 帐户的功能,而我看不到如何在服务器端实现这一点?
    • 另外,对任何混淆表示歉意,但我的应用程序是指 Web 应用程序。我无权访问 webView 属性或 config.xml 文件
    • @user7070541 JavaScript 仍然能够设置用户代理...而“将 OAuth 2.0 用于 Web 服务器应用程序”解释了它如何与服务器端身份验证流程一起工作。使用更新的用户代理字符串,您不必这样做。这是一个例子ctrlq.org/code/20363-google-api-php-oauth2-example
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多