Flutter:Oauth2-重定向uri问题

时间:2019-10-04 23:39:07

标签: flutter oauth oauth-2.0 spotify

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

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

<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>

enter image description here

3 个答案:

答案 0 :(得分:1)

MERGE INTO Target d2 USING (SELECT Unique_id, Part_no, Country_code FROM Source UNION ALL SELECT a.Unique_id, a.Part_no, a.Country_code FROM Target a LEFT JOIN Source b ON a.Unique_id=b.Unique_id WHERE b.Unique_id IS NULL ) d ON (d2.Unique_id=d.Unique_id) WHEN NOT MATCHED THEN INSERT(Unique_id, Part_no, Country_code, Action) VALUES(d.Unique_id, d.Part_no, d.Country_code, 'I') WHEN MATCHED THEN UPDATE SET d2.Action = 'U', d2.Country_code = d.Country_code; -- DELETE WHERE d2.loc='DELETE ME'; 应该是callbackUrlSchemepartyai应该是redirect_uri,AndroidManifest.xml android:scheme值应该是partyai:/

答案 1 :(得分:1)

使用此代码

void authenticate() async {
  // Present the dialog to the user
  final result = await FlutterWebAuth.authenticate(
    url:
        "https://accounts.spotify.com/authorize?client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&redirect_uri=yourname:/&scope=user-read-currently-playing&response_type=token&state=123",
    callbackUrlScheme: "yourname",
  );

// Extract token from resulting url
  final token = Uri.parse(result);
  String at = token.fragment;
  at = "http://website/index.html?$at"; // Just for easy persing
  var accesstoken = Uri.parse(at).queryParameters['access_token'];
  print('token');
  print(accesstoken);
}

添加到AndroidManifest.xml android \ app \ src \ main

                 ...
        </activity>
        <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="yourname" />
            </intent-filter>
            </activity>
    </application>
</manifest>

答案 2 :(得分:-1)

使用flutter_web_auth插件在flutter中从身份验证页面导航回应用程序时,我也遇到问题。

根据我的理解,每件事都可以正常运行,但它并没有导航回Flutter应用程序。

问题:以下代码中的.CallbackActivity是什么

 <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="yourname" />
        </intent-filter>
        </activity>