【问题标题】:The argument type 'Set<String>' can't be assigned to the parameter type 'Map<String, String>'参数类型“Set<String>”不能分配给参数类型“Map<String, String>”
【发布时间】:2021-09-06 23:27:03
【问题描述】:

我正在尝试在我的 Flutter 应用程序中使用 Auth0 创建用户身份验证系统。 Auth0 REST 文档给出了一个 cURL 的示例,但我没有找到任何可以完成 cURL 工作的颤振包。所以,我使用了http。 代码如下:

  Future<String> getToken(String userId) async {
    final response = await http.post(
      Uri.parse('https://my-auth0-subdomain.auth0.com/oauth/token'),  // I used my real subdomain
      body: jsonEncode({
        'grant_type=client_credentials',
        'client_id=my_project_client_id',  // I used my real client id
        'client_secret=my_project_client_secret',  // I used my real client secret
        'audience=https://my-auth0-subdomain.auth0.com/api/v2/'  // I used my real subdomain
      }),
      headers: {
        'content-type: application/x-www-form-urlencoded'
      },
    );
    final token = jsonDecode(response.body)["access_token"];

    return token;
  }

这给了我一个错误,The argument type 'Set&lt;String&gt;' can't be assigned to the parameter type 'Map&lt;String, String&gt;'. 在第 10 行 (headers: {...})。
我可以通过使用 headers: {'content-type': 'application/x-www-form-urlencoded'}, 来解决此错误。
但这会给出来自 Auth0 {"error":"access_denied","error_description":"Unauthorized"} 的错误。 API 设置正确,因为在运行时

curl --request POST \
  --url 'https://my-auth0-subdomain.auth0.com/oauth/token' \
  --header "content-type: application/x-www-form-urlencoded" \
  --data grant_type=client_credentials \
  --data 'client_id=my_project_client_id' \
  --data client_secret=my_project_client_secret \
  --data 'audience=https://my-auth0-subdomain.auth0.com/api/v2/'

它返回一个"access_token""scope""expires_in""token_type"

请帮忙。这很重要。
在此先感谢:)

【问题讨论】:

  • 使用'grant_type':'client_credentials' 代替'grant_type=client_credentials'。您应该像这样更改每个值
  • @MidhunMP 但是如果我这样做,那么响应是{"error":"access_denied","error_description":"Unauthorized"}。我认为 Auth0 只接受 "content-type: application/x-www-form-urlencoded" 而不是 "content-type": "application/x-www-form-urlencoded"
  • 为什么将正文作为 jsonEncode 发送并将标头内容类型设置为 application/x-www-form-urlencoded 而不是 application/json
  • @MidhunMP 感谢您的建议!我将您的建议与我的代码混合在一起,它奏效了!不知道怎么样????但它有效
  • @Dev Auth0 说它严格要求 application/x-www-form-urlencoded 并且它不会接受任何其他标头类型

标签: flutter jwt auth0 flutter-http


【解决方案1】:

尝试使用 url 编码发送数据:

Map<String, String> myBody= {

'grant_type' : 'client_credentials',
        'client_id' : 'my_project_client_id',  // I used my real client id
        'client_secret' : 'my_project_client_secret',  // I used my real client secret
        'audience ' : 'https://my-auth0-subdomain.auth0.com/api/v2/'  // I used my real subdomain     
};

Future<String> getToken(String userId) async {
    final response = await http.post(
      Uri.parse('https://my-auth0-subdomain.auth0.com/oauth/token'),  // I used my real subdomain
      body: myBody,
      headers: {
        'content-type: application/x-www-form-urlencoded'
      },
    );
    final token = jsonDecode(response.body)["access_token"];

    return token;
  }

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 2021-08-20
    • 2023-01-22
    • 2020-12-15
    • 2021-10-02
    • 2023-04-05
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多