Phonegap 3.3 inappbrowser事件不会激发

时间:2014-07-06 08:02:34

标签: cordova

我已经对这个问题进行了搜索,但严重的是他们无法帮助我。

我已经坚持了3,4天。

我使用Visual Studio 2012,DevExtreme 14.1和PhoneGap 3.3

我有一个按钮,可以通过Google Plus登录:

<button class="btn btn-danger btn-block" onclick="callGoogle()">@LoginWithGooglePlus</button>

然后我有一个脚本来执行这样的Authenticate任务:

var googleapi = {
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=yes');


    $(authWindow).on('loadstart', 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;
var UserData = null;

function callGoogle() {

//  alert('starting');
googleapi.authorize({
    client_id: '1045340154880-g1givpo92t41f56npo9c2pcos75brop8.apps.googleusercontent.com',
    client_secret: 'Xmfr1uiYru5d6zSGY36nAbuL',
    redirect_uri: 'http://localhost/oauth2callback',
    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);
    console.log(JSON.stringify(data));
    getDataProfile();


});

}
// This function gets data of user.
function getDataProfile() {
var term = 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;

        console.log(JSON.stringify(data));
        // Save the userprofile data in your localStorage.
        localStorage.gmailLogin = "true";
        localStorage.gmailID = data.id;
        localStorage.gmailEmail = data.email;
        localStorage.gmailFirstName = data.given_name;
        localStorage.gmailLastName = data.family_name;
        localStorage.gmailProfilePicture = data.picture;
        localStorage.gmailGender = data.gender;
    }
});
disconnectUser();
}
function disconnectUser() {
var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' + accessToken;

// Perform an asynchronous GET request.
$.ajax({
    type: 'GET',
    url: revokeUrl,
    async: false,
    contentType: "application/json",
    dataType: 'jsonp',
    success: function (nullResponse) {
        // Do something now that user is disconnected
        // The response is always undefined.
        accessToken = null;
        console.log(JSON.stringify(nullResponse));
        console.log("-----signed out..!!----" + accessToken);
    },
    error: function (e) {
        // Handle the error
        // console.log(e);
        // You could point users to manually disconnect if unsuccessful
        // https://plus.google.com/apps
    }
});
}

问题是:当我尝试通过Google Plus帐户登录时,我可以登录,但当他们要求我同意App访问帐户信息时,我点击同意,它会重定向到localhost并说:无法加载此

(抱歉,我无法添加图片) 但这是一个重定向的网址:

http://localhost/oauth2callback?code=4/RlwrXKHntvozNVo-IJNCtA_A-lAI.4nMvnj_fO_UcdJfo-QBMszsQPVcVjgI&authuser=0&num_sessions=1&prompt=consent&session_state=eb1f14e664ff0afed13ae7954898c6a099f3b35a..142d

错误是:此网页不可用

我做了一项研究,我知道自己被卡住了,因为InAppBrowser事件就像&#34; loadstart&#34;不能开火。

然后我尝试构建一个本机包作为Zip,并修改config.xml,添加行

<gap:plugin name="org.apache.cordova.inappbrowser"/>

,但结果没有变化。

请提示我!

非常感谢,任何帮助都会很感激!

编辑:这是构建Native包(Zip)后的config.xml

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets"
    xmlns:gap="http://phonegap.com/ns/1.0"
    id=""
    version = "1.0">
    versionCode = "1">
    <name></name>

    <description>DevExtreme based multiplatform application.</description>

    <author href="example.com" email="mail@example.com">PhoneGap User</author>

    <preference name="phonegap-version" value="3.3.0"/>
    <preference name="orientation" value="portrait"/>

    <access origin="*"/>

    <feature name="http://api.phonegap.com/1.0/file"/>
    <feature name="http://api.phonegap.com/1.0/geolocation"/>
    <feature name="http://api.phonegap.com/1.0/network"/>

    <gap:plugin name="org.apache.cordova.core.file"/>
    <gap:plugin name="org.apache.cordova.core.geolocation"/>
    <gap:plugin name="org.apache.cordova.inappbrowser" version="0.3.3" /> 

    <icon src="res/PackageIcons.Android.ic_launcher_36_36.png" gap:platform="android" gap:density="ldpi" />
    <icon src="res/PackageIcons.Android.ic_launcher_48_48.png" gap:platform="android" gap:density="mdpi" />
    <icon src="res/PackageIcons.Android.ic_launcher_72_72.png" gap:platform="android" gap:density="hdpi" />
    <icon src="res/PackageIcons.Android.ic_launcher_96_96.png" gap:platform="android" gap:density="xhdpi" />


    <gap:splash src="res/PackageIcons.Android.splash_200_320.png" gap:platform="android" gap:density="ldpi" />
    <gap:splash src="res/PackageIcons.Android.splash_320_480.png" gap:platform="android" gap:density="mdpi" />
    <gap:splash src="res/PackageIcons.Android.splash_480_800.png" gap:platform="android" gap:density="hdpi" />
    <gap:splash src="res/PackageIcons.Android.splash_720_1280.png" gap:platform="android" gap:density="xhdpi" />

2 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题。我发现inappBrowser插件丢失了。确保安装inAppbrowser插件。供参考click here

仅供参考我使用cordova cli安装了插件

答案 1 :(得分:0)

也许它是服务器身份验证方法,而不是localhost,您应该将回调发送到您的服务器?