【问题标题】:discord not valid formated well redirect uri不和谐无效格式化好重定向uri
【发布时间】:2022-10-14 19:57:08
【问题描述】:

嗨,与标题相同,尝试获取承载令牌不和谐时出现错误,我使用颤振 web auth_2,网页上出现错误,在颤振 web auth_2 中说放置另一个名为 com.linusu 的活动,在我的 android 中添加

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart'; 在此处输入代码`import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';

   enter code here

`enter code here`import 'dart:convert' show jsonDecode;
`enter code here`import 'package:http/http.dart' as http;

// App specific variables
final googleClientId = 'client';
final callbackUrlScheme = 'http://com.example.sabadospotify';

// Construct the url
final url = Uri.https('discord.com', '/oauth2/authorize', {
  'response_type': 'code',
  'client_id': googleClientId,
  'client_secret': 'secret',
  'redirect_uri': '$callbackUrlScheme:/',
  'scope': 'email',
});

// Present the dialog to the user
  Future<String> getGoogleAuthCode() async {
  final result = await FlutterWebAuth2.authenticate(
    url: url.toString(),
    callbackUrlScheme: callbackUrlScheme,
  );

  // Extract the code from the response URL
  final code = await FlutterWebAuth2.authenticate(url: url.toString(), callbackUrlScheme: callbackUrlScheme);
  return code;
  return Future.value(code);

}

String casteo= getGoogleAuthCode() as String;
//final result = await FlutterWebAuth2.authenticate(url: url.toString(), callbackUrlScheme: callbackUrlScheme);

// Extract code from resulting url
final result1 = Uri.parse(casteo).queryParameters['code'];
print(result1) {
  // TODO: implement print
  throw UnimplementedError();
}

Future<String> finalresponse()async {
  final response = await http.post(
    Uri.parse('https://discord.com/api/oauth2/token'),
    body: {
      'code': result1,
      'client_id': googleClientId,
      'redirect_uri': '$callbackUrlScheme:/',
      'grant_type': 'authorization_code',
    },
  );

  // Extract the access token from the response
  final body = jsonDecode(response.body);
  final accessToken = jsonDecode(response.body)['access_token'] as String;
  return accessToken;
}

void main() {
  runApp(miercoles());
}



class  miercoles extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Google Sign In'),
        ),
        body: Center(
          child: ElevatedButton(
            child: Text('Sign in with Google'),
            onPressed: () async {
              // Get the auth code
              getGoogleAuthCode();
              finalresponse();
            },
          ),
        ),
      ),
      );
  }

  Future<String>exchangeCodeForToken(String code) async {
    final response = await http.post(
      Uri.parse('https://discord.com/api/oauth2/token'),
      body: {
        'code': code,
        'client_id': googleClientId,
        'client_secret': 'secret',
        'redirect_uri': '$callbackUrlScheme:/',
        'grant_type': 'authorization_code',
      },
    );

    // Extract the access token from the response
    final body = jsonDecode(response.body);
    final accessToken = jsonDecode(response.body)['access_token'] as String;
    return accessToken;
  }
}

我的 android manifest 写着 readme pub dev

<activity
       android:name="com.linusu.flutter_web_auth_2.CallbackActivity"
       android:exported="true">
       <intent-filter android:label="flutter_web_auth_2">
           <action android:name="android.intent.action.VIEW" />
           <category android:name="android.intent.category.DEFAULT" />
           <category android:name="android.intent.category.BROWSABLE" />
           <data android:scheme="http://com.example.sabadospotify" />
       </intent-filter>
   </activity>

谢谢!

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您需要像这样更改您的 android.manifest 文件

    <data android:scheme="http" android:host="localhost:3000" />
    

    并像这样添加您的不和谐服务器重定向网址

    http://localhost:3000/auth
    

    并像这样在应用程序中添加您的重定向网址

    http://localhost:3000/auth
    

    另外,如果您没有成功,则删除 callback_url 并发送 null 您可以找到有关颤振深度链接的更多详细信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-13
      • 2022-08-08
      • 2014-10-03
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多