flutter 阿里云盘API调用

flutter 阿里云盘API调用

import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart' as http;


class YunPanUpload {
  final aliPanHost = "https://openapi.alipan.com";

  //获取用户信息
  Future<Map<String, dynamic>> getUserInfo(accessToken) async {
    final resp =
        await http.get(Uri.parse('$aliPanHost/oauth/users/info'), headers: {
      'Authorization': 'Bearer $accessToken',
    });
    Map<String, dynamic> map = jsonDecode(resp.body);
    return map;
  }

  //获取用户drive信息
  Future<Map<String, dynamic>> getDriverInfo(accessToken) async {
    final resp = await http.post(
      Uri.parse("$aliPanHost/adrive/v1.0/user/getDriveInfo"),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer $accessToken',
      },
    );
    Map<String, dynamic> json = jsonDecode(resp.body);
    return json;
  }

  //创建文件
  Future<Map<String, dynamic>> createFile(accessToken,Map<String,dynamic> body) async {
    final resp = await http.post(
      Uri.parse("$aliPanHost/adrive/v1.0/openFile/create"),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer $accessToken',
      },
      body: jsonEncode(body)
    );
    Map<String, dynamic> json = jsonDecode(resp.body);
    return json;
  }

  //造多级文件夹
  Future<Map<String, dynamic>> createDir(accessToken,drive_id) async{
    final date = DateTime.now();
    int year = date.year;
    int month = date.month;
    int day = date.day;
    final path = "snapshot";
    Map<String,dynamic> bodyf = {
      'drive_id': drive_id,
      'parent_file_id': "root",
      'name': path,
      'type': 'folder',
      'check_name_mode': "refuse",
    };
    final uploadInfof = await createFile(accessToken,bodyf);
    bodyf['parent_file_id'] = uploadInfof['file_id'];
    bodyf['name'] = year;
    final uploadInfof2 = await createFile(accessToken,bodyf);
    bodyf['parent_file_id'] = uploadInfof2['file_id'];
    bodyf['name'] = month;
    final uploadInfof3 = await createFile(accessToken,bodyf);
    bodyf['parent_file_id'] = uploadInfof3['file_id'];
    bodyf['name'] = day;
    final uploadInfof4 = await createFile(accessToken,bodyf);
    return uploadInfof4;
  }


  //上传文件
  Future<http.Response> uploadFile(String url, File file) async {

    HttpClient httpClient = new HttpClient();
    HttpClientRequest requests = await httpClient.putUrl(Uri.parse(url));

    requests.add(file.readAsBytesSync());

    final response = await requests.close();
    print('Response status: ${response.statusCode}');
    await response.transform(utf8.decoder).forEach(print);
    httpClient.close();

    return http.Response(response.reasonPhrase, response.statusCode);
  }

  //完成上传
  Future<Map<String,dynamic>> complete(accessToken,drive_id,file_id,upload_id) async{
    final resp = await http.post(
        Uri.parse("$aliPanHost/adrive/v1.0/openFile/complete"),
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer $accessToken',
        },
        body: jsonEncode({
          'drive_id':drive_id,
          'file_id':file_id,
          'upload_id':upload_id
        })
    );
    Map<String, dynamic> json = jsonDecode(resp.body);
    return json;
  }

  //创建分享链接
  Future<Map<String,dynamic>> createShare(accessToken,drive_id,fileIdList,expiration,sharePwd) async{
    final resp = await http.post(
        Uri.parse("$aliPanHost/adrive/v1.0/openFile/createShare"),
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer $accessToken',
        },
        body: jsonEncode({
          'driveId':drive_id,
          'fileIdList':fileIdList,
          'expiration':expiration,
          'sharePwd':sharePwd
        })
    );
    Map<String, dynamic> json = jsonDecode(resp.body);
    return json;
  }

  //刷新access_token
  Future<Map<String,dynamic>> refreshToken(client_id,client_secret,refresh_token) async{
    final resp = await http.post(
        Uri.parse("$aliPanHost/oauth/access_token"),
        headers: {
          'Content-Type': 'application/json',
        },
        body: jsonEncode({
          'client_id':client_id,
          'grant_type':'refresh_token',
          'client_secret':client_secret,
          'refresh_token':refresh_token,
        })
    );
    Map<String, dynamic> json = jsonDecode(resp.body);
    return json;
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tutuxfsh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值