使用Dart GDrive api使用授权的GET请求下载文件 [英] Downloading a file using Dart GDrive api with authorized GET request

查看:118
本文介绍了使用Dart GDrive api使用授权的GET请求下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Dart中编码,需要使用OAuth2.0从google GDrive下载文件(即图像文件)。我在我的代码中,在我有使用Dart drive_v2_api_browser客户端库后的downloadURL。



我试着直接传递到 src 属性

  _image = new ImageElement :file.downloadUrl,width:file.imageMediaMetadata.width,height:file.imageMediaMetadata.height); 
_image.onLoad.listen(onData,onError:onError,onDone:onDone,cancelOnError:true);

但产生了403禁止错误。这是我意识到我需要进行授权 GET 请求。所以我尝试按照Dart Auth pub包中列出的示例 https://pub.dartlang.org/packages/ google_oauth2_client ,但我不明白它要求什么。



这是我想要的:

  var auth = new oauth.SimpleOAuth2(_picker.Token.data); 
var request = new HttpRequest();
request.onLoad.listen(onData_Request,onError:onError_Request,onDone:onDone_Request,cancelOnError:true);
request.open(request,file.downloadUrl);
auth.authenticate(request).then((request)=> request.send());

但它会给我一个错误:



<



任何人都有一个工作示例通过Dart GDrive api相对于OAuth2.0下载文件?



更新:GünterZöchbauer帮助后, blob as such:



将此行添加到请求对象:

  _downloadRequest.responseType =blob; 

允许我使用文件读取器:



< pre $ {
Blob response = _downloadRequest.response;};如果没有,则返回false。
final FileReader reader = new FileReader();

reader.onLoad.listen((e){
_handleData(reader);
});
reader.readAsArrayBuffer(response);
}

void _handleData(FileReader reader){
Uint8List uintlist = new Uint8List.fromList(reader.result);
String charcodes = new String.fromCharCodes(uintlist);
_loadImage(_image,charcodes,225,225);
}

void _loadImage(ImageElement imageE,String data,int iWidth,int iHeight){
_imageAsbase64 = window.btoa(data);

_image = new ImageElement(src:data:image / png; base64,+ _imageAsbase64,width:iWidth,height:iHeight);
_image.onLoad.listen(onData,onError:onError,onDone:onDone,cancelOnError:true);
}

void onData(Event e){
print(success:);
_context.drawImage(_image,0,0);
}


解决方案

code> request 不是有效的HTTP方法。
您需要像 GET PUT POST DELETE ,...



另请参阅 http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html


I'm coding in Dart and need to download a file (i.e. an image file) from google GDrive using OAuth2.0. I am at the point in my code where I have the downloadURL after having used the Dart drive_v2_api_browser client library.

I tried directly passing this to the "src" attribute

_image = new ImageElement(src: file.downloadUrl, width: file.imageMediaMetadata.width, height: file.imageMediaMetadata.height);
_image.onLoad.listen(onData, onError: onError, onDone: onDone, cancelOnError: true);

but that yielded a 403 forbidden error. That's when I realized I need to make an "authorized GET request". So I tried following the example listed on the Dart Auth pub package https://pub.dartlang.org/packages/google_oauth2_client, but I don't understand what it asking for.

This is what I am trying:

var auth = new oauth.SimpleOAuth2(_picker.Token.data);
var request = new HttpRequest();
request.onLoad.listen(onData_Request, onError: onError_Request, onDone: onDone_Request, cancelOnError: true);
request.open("request", file.downloadUrl);
auth.authenticate(request).then((request) => request.send());

but it keeps giving me an error:

token.... Method request is not allowed by Access-Control-Allow-Methods.

Does anyone have a working example of downloading a file through the Dart GDrive api relative to OAuth2.0?

Update: After Günter Zöchbauer help I was able to continue and convert the blob as such:

Adding this line to the request object:

_downloadRequest.responseType = "blob";

Allowed me to use a file reader:

void onData_Request(Event e) {
  Blob response = _downloadRequest.response;
  final FileReader reader = new FileReader();

  reader.onLoad.listen((e) {
        _handleData(reader);
      });
  reader.readAsArrayBuffer(response);
}

void _handleData(FileReader reader) {
  Uint8List uintlist = new Uint8List.fromList(reader.result);
  String charcodes = new String.fromCharCodes(uintlist);
  _loadImage(_image, charcodes, 225, 225);
}

void _loadImage(ImageElement imageE, String data, int iWidth, int iHeight) {
  _imageAsbase64 = window.btoa(data);

 _image = new ImageElement(src: "data:image/png;base64," + _imageAsbase64, width: iWidth, height: iHeight);
 _image.onLoad.listen(onData, onError: onError, onDone: onDone, cancelOnError: true);
}

void onData(Event e) {
  print("success: ");
  _context.drawImage(_image, 0, 0);
}

解决方案

As the error message says request is not a valid HTTP method. You need something like GET, PUT, POST, DELETE, ...

see also http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

这篇关于使用Dart GDrive api使用授权的GET请求下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆