Open InAppBrowser with Authorization Header

折月煮酒 提交于 2019-12-24 13:39:38

问题


For my Ionic2 app i have a requirement of downloading invoices but only for authenticated users. Is there any way to do it?

I have installed the InAppBrowser cordova plugin but it seems like it cannot be used for sending Authorization Header with request.


回答1:


This work for me: Look for the (YourProject)\plugins\cordova-plugin-inappbrowser\src\android\InAppBrowser.java file and edit it.

In the onReceivedHttpAuthRequest at the end of the file you should put something like this.

    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {

        // Check if there is some plugin which can resolve this auth challenge
        String UserName = getUser();
        String Password = getPass();

        if (UserName != null && Password!= null) {
            handler.proceed(UserName, Password);
            return;
        }

You can replace the getUser() and getPass() with your username and password to try if is working, if it does. Create methods to retrieve the data from the inputs.

In my case I use this:

public void setUserPass(String username, String password) {
    this.USUARIO = username;
    this.CONTRASENA = password;
}    

public String getUser() {
    return this.USUARIO;
}

public String getPass() {
    return this.CONTRASENA;
}

Outside the method InAppBrowserClient that contains the onReceivedHttpAuthRequest method. Then almost in the top of the document I wrote this in the execute method (setUserPass(args.getString(3), args.getString(4));)

public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals("open")) {
        this.callbackContext = callbackContext;
        final String url = args.getString(0);
        setUserPass(args.getString(3), args.getString(4));
        String t = args.optString(1);
        if (t == null || t.equals("") || t.equals(NULL)) {
            t = SELF;
        }
        final String target = t;

Then you have to edit the (YourProject)\plugins\cordova-plugin-inappbrowser\www\inappbrowser.js file at the end.

module.exports = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
    // Don't catch calls that write to existing frames (e.g. named iframes).
    if (window.frames && window.frames[strWindowName]) {
        var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open');
        return origOpenFunc.apply(window, arguments);
    }

    strUrl = urlutil.makeAbsolute(strUrl);
    var iab = new InAppBrowser();

    callbacks = callbacks || {};
    for (var callbackName in callbacks) {
        iab.addEventListener(callbackName, callbacks[callbackName]);
    }

    var cb = function(eventname) {
       iab._eventHandler(eventname);
    };

    strWindowFeatures = strWindowFeatures || "";

    exec(cb, cb, "InAppBrowser", "open", [strUrl, strWindowName, strWindowFeatures[0], strWindowFeatures[1], strWindowFeatures[2]]);
    return iab;
};

Finally, in the index.js or any js that control your app use the same call but you add your login data using the feature part as an array and like this:

window.open(url, '_blank', ['toolbar=no,presentationstyle=fullscreen,location=no,hardwareback=yes,hidden=no', username, password]);

To activate all this you should build the project in the console.

Hope this help somebody.




回答2:


The headers could be sent along with the request bro., Did you try like this:

import {Http, Headers} from 'angular2/http';


    var headers = new Headers();
    headers.append('Authorization', 'Bearer ' + this.authToken));
    this.http.get('http://localhost:3333/getinfo', {headers: headers}).subscribe(data => {
    console.log(data);

Please have a look at - http://tphangout.com/?p=162 (It gives simple instructions on making authorized requests). Hope this helps you.



来源:https://stackoverflow.com/questions/36041760/open-inappbrowser-with-authorization-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!