如何在 flutter 的 http 请求中传递访问令牌?
how to pass access token in a http request with flutter?
我想使用营销 API 创建一个活动,这是 Curl 代码,我想转换成一个 http post 请求:
和我的 HTTP 请求与模型 class
Future<Campaign> createCampaign(String name,String objective,String
status) async {
final http.Response response = await http.post(
'https://graph.facebook.com/v7.0/act_<AD_ACCOUNT_ID>/campaigns',
headers: {HttpHeaders.authorizationHeader: "Basic },
body: jsonEncode(<String, String>{
'name': name,
'objective': objective,
'status': status
}),
);
if (response.statusCode == 201) {
return Campaign.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to create Campaign.');
}
}
class Campaign {
final String name;
final String objective;
final String status;
final Map <String,dynamic> special_ad_categories;
final Map<String,dynamic> access_token;
Campaign({this.name,this.objective,this.status,this.special_ad_categories,
this.access_token});
factory Campaign.fromJson(Map<String, dynamic> json) {
return Campaign(
name: json['name'],
objective: json['objective'],
status: json['status'],
special_ad_categories: json['special_ad_categories'],
access_token: json['access_token'],
);
}
}
所有参数都在正文中。像这样尝试:
Future<http.Response> fetchAlbum() {
return http.post(
'https://your-url',
body: {
'name': name,
'objective': objective,
'status': status,
'special_ad_categories': [],
'access_token': accessToken,
},
);
}
尝试使用 dio 包。它有API发送表单数据。
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart' as dio;
const AD_ACCOUNT_ID = '...';
const ACCESS_TOKEN = '...';
Future<Campaign> createCampaign(String name, String objective, String status, String categories) async {
try {
final formData = dio.FormData.fromMap({
'name': name,
'objective': objective,
'status': status,
'special_ad_categories': categories,
'access_token': ACCESS_TOKEN
});
final response = await dio.Dio().post(
'https://graph.facebook.com/v7.0/act_$AD_ACCOUNT_ID/campaigns',
data: formData,
);
if (response.statusCode == HttpStatus.created) {
return Campaign.fromJson(jsonDecode(response.data));
} else {
throw Exception('Failed to create Campaign.');
}
} on dio.DioError {
throw Exception('Failed to create Campaign.');
}
}
// example of calling: createCampaign('test', 'LINK_CLICKS', 'PAUSED', 'NONE');
不要忘记替换 AD_ACCOUNT_ID
和 ACCESS_TOKEN
。
我想你错过了
中的帐户 ID
'https://graph.facebook.com/v7.0/act_<AD_ACCOUNT_ID>/campaigns'
我想使用营销 API 创建一个活动,这是 Curl 代码,我想转换成一个 http post 请求:
和我的 HTTP 请求与模型 class
Future<Campaign> createCampaign(String name,String objective,String
status) async {
final http.Response response = await http.post(
'https://graph.facebook.com/v7.0/act_<AD_ACCOUNT_ID>/campaigns',
headers: {HttpHeaders.authorizationHeader: "Basic },
body: jsonEncode(<String, String>{
'name': name,
'objective': objective,
'status': status
}),
);
if (response.statusCode == 201) {
return Campaign.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to create Campaign.');
}
}
class Campaign {
final String name;
final String objective;
final String status;
final Map <String,dynamic> special_ad_categories;
final Map<String,dynamic> access_token;
Campaign({this.name,this.objective,this.status,this.special_ad_categories,
this.access_token});
factory Campaign.fromJson(Map<String, dynamic> json) {
return Campaign(
name: json['name'],
objective: json['objective'],
status: json['status'],
special_ad_categories: json['special_ad_categories'],
access_token: json['access_token'],
);
}
}
所有参数都在正文中。像这样尝试:
Future<http.Response> fetchAlbum() {
return http.post(
'https://your-url',
body: {
'name': name,
'objective': objective,
'status': status,
'special_ad_categories': [],
'access_token': accessToken,
},
);
}
尝试使用 dio 包。它有API发送表单数据。
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart' as dio;
const AD_ACCOUNT_ID = '...';
const ACCESS_TOKEN = '...';
Future<Campaign> createCampaign(String name, String objective, String status, String categories) async {
try {
final formData = dio.FormData.fromMap({
'name': name,
'objective': objective,
'status': status,
'special_ad_categories': categories,
'access_token': ACCESS_TOKEN
});
final response = await dio.Dio().post(
'https://graph.facebook.com/v7.0/act_$AD_ACCOUNT_ID/campaigns',
data: formData,
);
if (response.statusCode == HttpStatus.created) {
return Campaign.fromJson(jsonDecode(response.data));
} else {
throw Exception('Failed to create Campaign.');
}
} on dio.DioError {
throw Exception('Failed to create Campaign.');
}
}
// example of calling: createCampaign('test', 'LINK_CLICKS', 'PAUSED', 'NONE');
不要忘记替换 AD_ACCOUNT_ID
和 ACCESS_TOKEN
。
我想你错过了
中的帐户 ID'https://graph.facebook.com/v7.0/act_<AD_ACCOUNT_ID>/campaigns'