我越来越“webView:didFailLoadWithError -1004:无法连接到服务器”错误,同时接受谷歌加认证之前获取配置文件数据。这些代码在以前正常工作。现在我面临着这些错误。不知道为什么我无法connect.Please帮助我摆脱这些错误。下面是我的代码为iPhone集成谷歌加Phonegap(3.4.0)为iOS。webView:didFailLoadWithError -1004:无法连接到服务器,而连接谷歌plus在Phonegap ios
var googleapi = {
//alert('ready');
authorize: function(options) {
var deferred = $.Deferred();
//Build the OAuth consent page URL
var authUrl = 'https://accounts.google.com/o/oauth2/auth?' + $.param({
client_id: options.client_id,
redirect_uri: options.redirect_uri,
response_type: 'code',
scope: options.scope
});
//Open the OAuth consent page in the InAppBrowser
var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=yes');
$(authWindow).on(' ', function(e) {
var url = e.originalEvent.url;
var code = /\?code=(.+)$/.exec(url);
var error = /\?error=(.+)$/.exec(url);
if (code || error) {
//Always close the browser when match is found
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) {
deferred.resolve(data);
$("#loginStatus").html('Name: ' + data.given_name);
}).fail(function(response) {
deferred.reject(response.responseJSON);
});
} else if (error) {
//The user denied access to the app
deferred.reject({
error: error[1]
});
}
});
return deferred.promise();
}
};
var accessToken;
function callGoogle()
{
alert('starting');
googleapi.authorize({
client_id: 'CLIENT_ID',
client_secret: 'CLIENT_SECRET',
redirect_uri: 'http://localhost',
scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
}).done(function(data) {
accessToken=data.access_token;
alert(accessToken);
// $loginStatus.html('Access Token: ' + data.access_token);
console.log(data.access_token);
getDataProfile();
});
}
function getDataProfile()
{
var term=null;
//accessToken=null;
//alert("getting user data="+accessToken);
$.ajax({
url:'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='+accessToken,
type:'GET',
data:term,
dataType:'json',
error:function(jqXHR,text_status,strError){
},
success:function(data)
{
var item;
var dat=data.properties;
alert("first name="+data.given_name+" last name="+data.family_name+" gender="+data.gender+" email="+data.email);
console.log(data);
}
});
}
即使我在谷歌加iOS登录相同的问题。如果你得到它,请发布解决方案。 – Rayon
试试这个答案http://stackoverflow.com/questions/23930744/how-to-use-google-login-api-with-cordova-phonegap/23931392#23931392 –