【问题标题】:Problem with Google API authentication in FlutterFlutter 中的 Google API 身份验证问题
【发布时间】:2020-03-17 18:18:04
【问题描述】:

只是我在 Flutter 中的身份验证有问题。我从文档中尝试了最后两种方法: [Google 新闻 API 文档][1]

我的代码:

  final String baseUrl = "https://newsapi.org/v2/";
  final String query = "top-headlines?country=gb&";
  final String authentication = ApiKey.apiKey;

  //For testing


  Future<List<Article>> getArticles() async {
    var response = await http.get(baseUrl + query, headers: <String, String>{
      "Authentication":authentication
    });
    List jsons = [];
    print(response.body);
  }

  Future<String> getSingleArticle(String url) async {
    var articleResponse = await http.get(url);
    String articleHTMLString = articleResponse.body;
    return articleHTMLString;

  }
}```



  [1]: https://newsapi.org/docs/authentication

【问题讨论】:

  • 我在运行后得到:> {"status":"error","code":"apiKeyMissing","message":"您的 API 密钥丢失。将其附加到 URL apiKey 参数,或使用 x-api-key HTTP 标头。"}
  • 当我使用 Postman 提出请求时,我得到了正确的答案。

标签: api flutter


【解决方案1】:

你应该这样做,可能会解决。

  Future<List<Article>> getArticles() async {
    var response = await http.get(baseUrl + query, headers: <String, String>{
      "x-api-key":authentication // <-- On your comment.
    });
    List jsons = [];
    print(response.body);
  }

【讨论】:

  • 感谢您的回复。不幸的是,在应用您的代码后,我得到: {"message":"The request is invalid."}
猜你喜欢
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
相关资源
最近更新 更多