我看到这个问题类似于另一个问题(webView:didFailLoadWithError -1004: Could not connect to the server while connecting google plus in 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版本调用window.open()调用,该调用首先通过loadstart处理程序使用原始URL,但它不包含任何代码,也没有错误,因此它只是通过。然后redirect_url出现,第二次调用loadstart处理程序(这是由InAppBrowser?)但是这次它有更短的redirect_url并附加了代码,因此代码成功用于获取“$”上的令牌.post“打电话。但是,在iOS上,没有第二次调用loadstart处理程序。
当我在Chrome调试器中运行它时,我没有错误,只是无声失败。在XCode调试器中,我得到如下错误:
2016-06-09 20:47:27.014 APass2 [675:398271]设置WebView的框架 到{{0,0},{320,524}} 2016-06-09 20:47:27.015 APass2 [675:398271] 将WebView的框架设置为{{0,0},{320,568}} 2016-06-09 20:47:27.026 APass2 [675:398271]线程警告:['InAppBrowser']采取 '39 .259033'ms。插件应该使用后台线程。 2016年6月9日 20:47:27.749 APass2 [675:398271] webView:didFailLoadWithError - -1004: 无法连接到服务器。 2016-06-09 20:47:28.955 APass2 [675:398271]错误内部导航被拒绝 - 不适合 URL = 'https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.joG9nQvYxYQ.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCPyXDgCg_S7GlvvvMpztuAZ6V0pEA#parent=file%3A%2F%2F&rpctoken=1268129019'
我的成功或失败回调均未被调用。
请帮忙!!!我现在完全不知所措。
谢谢, 爱德华
答案 0 :(得分:1)
首先,通过查看InAppBrowser文档,我了解到还有一个“loaderror”事件。仅在iOS上,对inAppBrowser.open()的调用导致调用“loaderror”处理程序。在“loaderror”处理程序中,我也能够抓取url,就像原始代码在“loadstart”上所做的那样。在Chrome和Safari中同时进行调试我能够看到“loadror”中的url与“loadstart”处理程序中的url完全相同,并且代码和错误的解析工作方式完全相同。因此,在第一次切割中,我以这种方式入侵并进入下一阶段(成功 - 有点)。然后我点击了与<access-navigation>
相关的另一个错误。谷歌搜索了更多,我发现在项目的根目录中的config.xml中有一个配置设置。
Googling指出有人说使用<allow-navigation href="*" />
显然,我对这个广泛的安全漏洞感到不满。
所以,最重要的是我需要添加Google api需要访问config.xml文件的URL,如下所示:
<allow-navigation href="https://accounts.google.com/*" />
<allow-navigation href="https://content.googleapis.com/*" />
我仍然需要清理代码,并且可能会简化“loaderror”处理程序中的错误处理,但我现在已经开始工作了!
最令人沮丧的是,在Android上根本不需要这个设置,所以我没有理由怀疑这是问题。
感谢那些花时间看这个的人!
爱德华