Flutter:Oauth2-重定向uri问题 [英] Flutter: Oauth2 - Problems with redirect uri

查看:133
本文介绍了Flutter:Oauth2-重定向uri问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Flutter应用中设置Spotify API的oAuth身份验证.我选择了flutter_web_auth 0.1.1软件包.到目前为止,我已经设法使用户可以登录到Spotify.登录后,应将用户重定向回我的应用程序.那行不通.Spotify始终将用户重定向到另一个网站,而不是重定向到应用程序.用户登录后如何关闭WebView并将用户重定向到我的应用程序?

I want to set up the oAuth authentication of the Spotify API in my Flutter app. I chose the flutter_web_auth 0.1.1 package. So far, I have managed that the user can log in to Spotify. After logging in, the user should be redirected back to my app. That does not work. Spotify always redirects the user to another website and not back to the app. How do I close the WebView after the user logging in and redirect the user to my app?

import 'package:flutter/material.dart';
import 'package:flutter_web_auth/flutter_web_auth.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    authenticate();
  }

  void authenticate() async {
    // Present the dialog to the user
    final result = await FlutterWebAuth.authenticate(
      url:
          "https://accounts.spotify.com/de/authorize?client_id=78ca499b2577406ba7c364d1682b4a6c&response_type=code&redirect_uri=https://partyai/callback&scope=user-read-private%20user-read-email&state=34fFs29kd09",
      callbackUrlScheme: "https://partyai/callback",
    );

// Extract token from resulting url
    final token = Uri.parse(result).queryParameters['token'];
    print('token');
    print(token);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Web Auth example'),
        ),
        body: Center(
          child: Text(
            'test',
          ),
        ),
      ),
    );
  }
}


android/app/src/main/AndroidManifest.xml


android/app/src/main/AndroidManifest.xml

<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
            <intent-filter android:label="flutter_web_auth">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https://partyai/callback" />
            </intent-filter>
        </activity>

在此处输入图片描述

推荐答案

callbackUrlScheme 应该是 partyai redirect_uri 应该是 partyai:/和AndroidManifest.xml android:scheme值应为 partyai .

The callbackUrlScheme should be partyai, the redirect_uri should be partyai:/ and the AndroidManifest.xml android:scheme value should be partyai.

这篇关于Flutter:Oauth2-重定向uri问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆