flutter_inappwebview icon indicating copy to clipboard operation
flutter_inappwebview copied to clipboard

Pass bearer authorization token

Open riccardopirani opened this issue 2 years ago • 1 comments

Hi the code below must display a webview with a pdf report, to open the web view I used the code below, how can I pass the bearer token? to authenticate the request

MY code:


class ReportView extends StatefulWidget {
  final Manutenzione man;
  final String token;
  final String url;

  const ReportView(this.man, this.token, this.url);

  @override
  createState() => ReportViewState();
}

class ReportViewState extends State<ReportView> {
  void _handleLoad(String value) {}
  final flutterWebviewPlugin = new FlutterWebviewPlugin();

  @override
  void initState() {

    super.initState();

    // Enable virtual display.
    flutterWebviewPlugin.close();
    log("url: " + widget.url + " token: " + widget.token);
    print("url: " + widget.url + " token: " + widget.token);
    URLLOAD = widget.url +
        "/services/rest/v3/reports/286763/Activity%20report.pdf?extension=pdf&parameters=%7B%22ProcessId%22%3A3027991%7D";
    print(URLLOAD);
    log(URLLOAD);
  }


  @override
  void dispose() {

    flutterWebviewPlugin.dispose();

    super.dispose();
  }

  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(

      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

  String url = "";
  double progress = 0;


  void _launchUrl(String url) async {
    print("_launchUrl");
    log("_launchUrl");
    print(url);
    log(url);
    if (!await launchUrl(Uri.parse(url))) throw 'Could not launch $url';
  }

  @override
  Widget build(BuildContext context) {
    log("url buid: " + URLLOAD);
    print("url buid: " + URLLOAD);
    return Scaffold(
        appBar: AppBar(
            leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              iconSize: 20.0,
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            centerTitle: true,
            title: Text('Report')),
        body: IndexedStack(index: 0, children: [
          Column(
            children: <Widget>[
              Expanded(
                child: InAppWebView(
          key: webViewKey,
          initialOptions: options,
          //pullToRefreshController: pullToRefreshController,
          onWebViewCreated: (controller) {
            controller.loadUrl(urlRequest:  URLRequest(url: Uri.parse(URLLOAD)));
            //webViewController.loadUrl(URLLOAD, headers: {"Cmdbuild-authorization": widgew});
            webViewController = controller;
          },
          onLoadStart: (controller, url) {
            setState(() {
              this.url = url.toString();
            });
          },
          androidOnPermissionRequest: (controller, origin, resources) async {
            return PermissionRequestResponse(
                resources: resources,
                action: PermissionRequestResponseAction.GRANT);
          }),
              ),
            ],
          )
        ]));
  }
}

riccardopirani avatar Jun 23 '22 11:06 riccardopirani

👋 @riccardopirani

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

github-actions[bot] avatar Jun 23 '22 11:06 github-actions[bot]

You can pass it like that:

initialUrlRequest: URLRequest( url: someUrl, headers: { HttpHeaders.authorizationHeader: yourToken, }, ),

piekarskipiotr avatar Sep 22 '22 10:09 piekarskipiotr

any update on this ?

gitsofkhaleel avatar Dec 12 '22 05:12 gitsofkhaleel

But when click in any link/navigate lose Authorization Header and shouldOverrideUrlLoading, shouldInterceptAjaxRequest and shouldInterceptFetchRequest is never triggered.

How could I keep header: Authorization Bearer in next navigate?

unilogica avatar Jun 08 '23 16:06 unilogica

any update on this?

julien-neon avatar Oct 06 '23 13:10 julien-neon