使用HTTP包访问颤振中的RESTAPI

使用HTTP包访问颤振中的RESTAPI,rest,flutter,spotify,Rest,Flutter,Spotify,我有一个RESTAPI,我需要从颤振应用程序访问它。它来自Spotify Web API。我将为此使用的终端命令是 curl -X "GET" "https://api.spotify.com/v1/me/following? type=artist&limit=50" -H "Accept: application/json" -H "Content-Type: application/json" -H

我有一个RESTAPI,我需要从颤振应用程序访问它。它来自Spotify Web API。我将为此使用的终端命令是

curl -X "GET" "https://api.spotify.com/v1/me/following?
type=artist&limit=50" -H "Accept: application/json" -H "Content-Type: 
application/json" -H "Authorization: Bearer (ACCESS TOKEN)"
这很有效。在flatter中,我将
import'包:http/http.dart'作为http
包导入,并导入
import'dart:convert'
。我还有一个
未来
,如下所示

Future<String> getData() async {
    http.Response response = await http.get(
        Uri.https("api.spotify.com", "v1/me/following?type=artist&limit=50"),
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json",
          "Authorization":
              "Bearer (ACCESS TOKEN)"
        });

    Map dataMap = jsonDecode(response.body);
    print(dataMap);
  }
Future getData()异步{
http.Response-Response=等待http.get(
https(“api.spotify.com”,“v1/me/following?type=artist&limit=50”),
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
“授权”:
“承载(访问令牌)”
});
Map dataMap=jsonDecode(response.body);
打印(数据地图);
}

这导致了
{error:{status:404,message:Service not found}}
,这很奇怪,因为它在终端中工作得很好。我做错了什么?

你需要保持简单。可能存在与URL相关的问题。试试这个:

Future<String> getData() async {
    http.Response response = await http.get(
        "https://api.spotify.com/v1/me/following?type=artist&limit=50",
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json",
          "Authorization":
              "Bearer (ACCESS TOKEN)"
        });

    Map dataMap = jsonDecode(response.body);
    print(dataMap);
  }

Future getData()异步{
http.Response-Response=等待http.get(
"https://api.spotify.com/v1/me/following?type=artist&limit=50",
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
“授权”:
“承载(访问令牌)”
});
Map dataMap=jsonDecode(response.body);
打印(数据地图);
}
NetService:

class NetService {
  static Future<T?> getJson<T>(String url, {int okCode = 200, String? authKey, Map<String, String>? headers}) {
    var localHeaders = <String, String>{};
    
    localHeaders['Accept'] = 'application/json';
    localHeaders['Content-Type'] = 'application/json';
    if (authKey != null) localHeaders['Authorization'] = 'Bearer $authKey';
    if (headers != null) localHeaders.addAll(headers);

    return http.get(Uri.parse(url), headers: localHeaders)
      .then((response) {
        if (response.statusCode == okCode) {
          return jsonDecode(response.body) as T;
        }
        PrintService.showDataNotOK(response);
        return null;
      })
      .catchError((err) => PrintService.showError(err));
  }
}

我不确定它最近是否被更改过,但是
.get
将Uri作为地址而不是字符串。因此,它给出了一个错误。将字符串更改为Uri.https('api.spotify.com','v1/me/following?type=artist&limit=50'),仍然会出现404错误。这里有一个用法:
import 'dart:async';

import 'package:_samples2/networking.dart';

class Spotify {
  static const _oAuthToken = 'XXXXYYYYYZZZZ';
  static const _url = 'https://api.spotify.com/v1';

  static FutureOr<void> getUsersFollowedArtists() async {
    await NetService.getJson<Map<String, dynamic>>(_url + '/me/following?type=artist&limit=50', authKey: _oAuthToken)
      .then((response) => print(response))
      .whenComplete(() => print('\nFetching done!'));
  }
}

void main(List<String> args) async {
  await Spotify.getUsersFollowedArtists();
  print('Done.');
}
{artists: {items: [], next: null, total: 0, cursors: {after: null}, limit: 50, href: https://api.spotify.com/v1/me/following?type=artist&limit=50}}

Fetching done!
Done.