reactjs - 使用 gapi.client.request 访问 Drive thumbnailLink 给出 404
问题描述
我目前正在开发一个 React 应用程序,它应该从用户的 Google Drive 加载一组(非公开)文件并使用 CSSbackground-image: url(...)
属性显示缩略图。我可以使用 GapiFiles:get
方法加载所有文件元数据,并使用gapi.client.setToken
. 我希望按如下方式加载缩略图:
function CollectionArtifact(props){
const [thumbnail, setThumbnail] = useState(null);
async function loadThumbnail(){
try {
const res = await window.gapi.client.request(props.artifact.thumbnailLink);
const data = await res.blob();
const localURL = URL.createObjectUrl(data);
setThumbnail(localURL);
catch(e){ console.log('Failed to load thumbnail', e); }
}
useEffect(() => {
if(props.apisLoaded){
loadThumbnail();
}
}, [props.apisLoaded]);
return (
...
<div style={{backgroundImage: `url(${thumbnail})`}}>
...
</div>
...
);
}
客户端在包装组件中初始化,如下所示:
window.gapi.load('client:auth2', () => {
window.gapi.client.init({
apiKey: developerKey,
clientId: clientId,
scope: 'https://www.googleapis.com/auth/drive'
}).then(() => {
window.gapi.client.load('drive', 'v3', () => {
if(props.onGapisLoad) props.onGapisLoad();
setInitialized(true);
});
});
});
但是,window.gapi.client.request
调用给出了Failed to load resource: the server responded with a status of 404
,而实际给出此错误的 URL 是代理请求https://docs.google.com/static/proxy.html?usegapi=1&...
。使用fetch
也不起作用,因为它会产生 CORS 错误。同时,如果我在登录到正确的 Google 帐户(并且没有其他帐户)时尝试thumbnailLink
在浏览器中访问,我可以毫无问题地看到图像。
通过授权请求加载缩略图的正确方法是什么?我已经看到其他建议生成 PDF 并使用它来创建公共缩略图的答案,但我想避免在用户的驱动器中创建额外的文件或使文件数据可公开访问。而且由于图像可以直接从浏览器加载,我认为有一种方法可以使用授权客户端访问它。
解决方案
推荐阅读
- swift - 我可以用 UIBezier 画一个椭圆的进度条吗
- android - 如何通过 adb shell 在 android 设备上使用用户名和密码而不是 root 设置 http 代理
- matlab - 将数字转换为用户生成的输入(MATLAB)
- vb.net - 如何使用代码更改 vbnet 中的按钮文本
- javascript - 浏览器中的OpenGL或其他图像处理,没有画布?
- c++ - 类似于 Knight-Tour-Problem 的回溯问题
- python - 为什么我在 if 语句中提出问题时会出现错误
- arduino - 在 if 语句中使用 & 运算符
- node.js - 从非 www 重定向到 www 域名后 cookie 丢失
- excel - 使用 vba-excel 从在线共享点文件夹下载 Zip 文件