繁体   English   中英

InAppBrowser OAUTH didFailLoadWithError 1004“无法连接到服务器。” <allow-navigation> 未设置网址”

[英]InAppBrowser OAUTH didFailLoadWithError 1004 “Could not connect to the server.” “<allow-navigation> not set for url”

我看到这个问题类似于另一个问题( web视图:didFailLoadWithError -1004:无法连接到服务器,同时连接谷歌加上PhoneGap的IOS ),但不知何故不同,因为我已经在代码行由行和它正在做同样的事情,但对我来说仍然不起作用。 也许还因为我使用的版本不同:iPhone 5S上的iOS 9.3.2,Cordova 6.1.1和cordova-plugin-inappbrowser 1.3.0。

我的代码在Android上效果很好,但在iPhone上效果不佳。 代码如下:

    var googleapi = {
    authorize: function(options) {
        var deferred = $.Deferred();
        var authUrl = GOOGLE_CLIENT_API_URL + $.param({
            client_id: options.client_id,
            redirect_uri: options.redirect_uri,
            response_type: 'code',
            scope: options.scope
        });
        console.log("authUrl: " + authUrl);
        var authWindow = window.open(authUrl, "_blank", "location=no,toolbar=no");  // for iOS add 'toolbar=no'

        //The recommendation is to use the redirect_uri "urn:ietf:wg:oauth:2.0:oob" 
        //which sets the authorization code in the browser's title. However, we can't 
        //access the title of the InAppBrowser. 
        // 
        //Instead, we pass a bogus redirect_uri of "http://localhost", which means the 
        //authorization code will get set in the url. We can access the url in the 
        //loadstart and loadstop events. So if we bind the loadstart event, we can 
        //find the authorization code and close the InAppBrowser after the user 
        //has granted us access to their data. 
        //
        // To clear the authorization, go to https://accounts.google.com/IssuedAuthSubTokens.
        $(authWindow).on('loadstart', function(e) {
            var url = e.originalEvent.url;
            var code = /\?code=(.+)$/.exec(url);
            var error = /\?error=(.+)$/.exec(url);

            if(code || error) {
                authWindow.close();
            }
            if (code) { 
                //Exchange the authorization code for an access token 
                $.post('https://accounts.google.com/o/oauth2/token', { 
                    code: code[1], 
                    client_id: options.client_id, 
                    client_secret: options.client_secret, 
                    redirect_uri: options.redirect_uri, 
                    grant_type: 'authorization_code' 
                }).done(function(data) {
                    // use the token we got back from oauth to setup the api.
                    gapi.auth.setToken(data);
                    // load the drive api.
                    loadDriveApi();
                    deferred.resolve(data); 
                }).fail(function(response) {
                    console.log("Posting code to Google failed.  No OAuth token will be returned.");
                    deferred.reject(response.responseJSON); 
                }); 
            } else if (error) { 
                //The user denied access to the app 
                console.log("Error retrieving code from Google.");
                deferred.reject({ 
                    error: error[1] 
                }); 
            } 
        });

        return deferred.promise();
    }
};

function checkAuth() {
    if(device.platform === 'browser') {
        console.log("calling gapi.auth.authorize()");
        gapi.auth.authorize(
        {
            'client_id' : CLIENT_ID,
            'scope' : SCOPES.join(' '),
            'immediate' : true
        }, handleAuthResult);
    } else {
        // because this is called only after deviceready(), InAppBrowser is initialized by now:
        console.log("using the InAppBrowser plugin to authenticate.");
        window.open = cordova.InAppBrowser.open;

        googleapi.authorize(
        {
            'client_id' : CLIENT_ID,
            'client_secret' : CLIENT_SECRET,
            'redirect_uri' : REDIRECT_URI,
            'scope' : SCOPES.join(' ')
        }, handleAuthResult);
    }
}

/**
 * Handle response from authorization server.
 *
 * @param {Object} authResult Authorization result.
 */
function handleAuthResult(authResult) {
    var authMenuItem = document.getElementById("menuitemenablegoogledrivebackup");
    if (authResult && !authResult.error) {
        // If already authorized, change menu option to allow user to deny Authorization
        authMenuItem.innerHTML = l("Disable Google Drive Backup");
        loadDriveApi();
    } else {
        alert("Authorization Error: " + authResult.error);
        console.log("inside handleAuthResult, authResult.error: " + authResult.error);

        // Show auth menu item, allowing the user to initiate authorization
        authMenuItem.innerHTML = l("Enable Google Drive Backup");
        // use the InAppBrowser to display the authorization window:
        // var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no');
        // or?
        // gapi.auth.authorize(
        //  {
        //      client_id: CLIENT_ID,
        //      scope: SCOPES.join(' '),
        //      immediate: false
        //  }, handleAuthResult)
    }
}

/**
 * Load Drive API client library.
 */
function loadDriveApi() {
    try {
    gapi.client.load('drive', 'v2', null).then(function(resp) {
        console.log("Google Drive API v2 loaded successfully.");
    }, function(reason) {
        alert('Google Drive API v2 FAILED to load: ' + reason.result.error.message);
        console.log('Google Drive aPI v2 FAILED to load: ' + reason.result.error.message);
    });
    } catch(err) {
        alert(err.message);
        console.log("Google Drive API v2 FAILED to load.  Exception: " + err.message);
    }
}

从调试中,我看到Android版本使用原始URL调用了window.open()调用,该调用首先通过loadstart处理程序进行处理,但是它不包含任何代码,也没有错误,因此可以通过。 然后REDIRECT_URL出现,在第二调用loadstart处理器(这是由InAppBrowser?),但这次它与附加代码的短REDIRECT_URL,所以代码,然后成功地得到了“$令牌.post”呼叫。 但是,在iOS上,没有第二次调用loadstart处理程序。

当我在Chrome调试器中运行它时,没有任何错误,只有无声的失败。 在XCode调试器中,出现以下错误:

2016年6月9日20:47:27.014 APass2 [675:398271] web视图的帧设置为{{0,0},{320,524}} 2016年6月9日20:47:27.015 APass2 [675:398271] web视图的帧设置为{{0,0},{320,568}} 2016年6月9日20:47:27.026 APass2 [675:398271] THREAD警告:[ 'InAppBrowser']把'39 0.259033' 毫秒。 插件应使用后台线程。 2016-06-09 20:47:27.749 APass2 [675:398271] webView:didFailLoadWithError--1004:无法连接到服务器。 2016年6月9日20:47:28.955 APass2 [675:398271] ERROR内部导航拒绝-未设置为URL =” https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_ %2Fscs%2Fapps静电%2F_%2Fjs%2FK%3Doz.gapi.en.joG9nQvYxYQ.O%2FM%3D__features __%2Fam%3DAQ%2Frt%3DJ%2FD%3D1%2Frs%3DAGLTcCPyXDgCg_S7GlvvvMpztuAZ6V0pEA#父=文件%3A%2F %2F&rpctoken = 1268129019 '

我的成功或失败回调均未调用。

请帮忙!!! 我现在很茫然。

谢谢,爱德华

首先,通过查看InAppBrowser文档,我了解到还有一个“ loaderror”事件。 仅在iOS上,对inAppBrowser.open()的调用导致调用“ loaderror”处理程序。 在“ loaderror”处理程序中,我也能够获取URL,就像原始代码在“ loadstart”上一样。 在Chrome和Safari中同时进行调试,我可以看到“ loaderror”中的网址与“ loadstart”处理程序中的网址完全相同,并且代码和错误的解析也完全相同。 因此,在第一个剪辑中,我以这种方式进行了破解,并进入了下一个阶段(成功-有点类似)。 然后我遇到了另一个与<access-navigation>有关的错误。 进一步搜索,我发现项目根目录中的config.xml中有可用的配置设置。

谷歌搜索使我指向一个说使用<allow-navigation href="*" />

显然,我对这个安全漏洞感到不满。

因此,最重要的是,我需要添加Google api访问config.xml文件所需的网址,如下所示:

<allow-navigation href="https://accounts.google.com/*" />
<allow-navigation href="https://content.googleapis.com/*" />

我仍然需要清理代码,并可能简化“ loaderror”处理程序中的错误处理,但是我现在可以正常工作了!

最令人沮丧的是,在Android上根本不需要此设置,因此我没有理由怀疑这是问题所在。

感谢您抽出宝贵的时间来研究这个问题!

爱德华

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM