我越來越「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 –