Image 从google api图像获取base64
我在我的网站上使用GoogleOAuth,让用户登录并分享自己的GoogleDrive中的一些照片。现在,谷歌为我检索了以下链接:Image 从google api图像获取base64,image,api,google-api,base64,Image,Api,Google Api,Base64,我在我的网站上使用GoogleOAuth,让用户登录并分享自己的GoogleDrive中的一些照片。现在,谷歌为我检索了以下链接: https://doc-0o-30-docs.googleusercontent.com/docs/securesc/dpstkbb0rv4jmf62kb8dc9m8ijh53ocp/1hotfemebktd56o93gcoi76k89edbtjp/1512662400000/05170378500578169489/05170378500578169489/1Mx
https://doc-0o-30-docs.googleusercontent.com/docs/securesc/dpstkbb0rv4jmf62kb8dc9m8ijh53ocp/1hotfemebktd56o93gcoi76k89edbtjp/1512662400000/05170378500578169489/05170378500578169489/1MxIJ5EBLcIVtqxjGi_ZoYYqpw1UQHBiCRA?e=download&gd=true
我已将此url更改为安全原因
问题是我想将此图像的base64保存在与用户相关的特定文件夹中,但出于某种原因。使用php函数file_get_contents()代码>它不工作
我尝试使用JS canvas获取img.toDataURL()
,但仍然失败
有办法吗?我发现了问题。要使用GoogleAPI获取内容,首先,我们应该设置正确的范围。在这种情况下,https://www.googleapis.com/auth/drive
因此,我们应该在get_file_contents()中设置传递参数“Authorization”和access_令牌的上下文。因此,代码将是:
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Bearer [ACCESS_TOKEN_HERE]"
)
));
$conteudo = file_get_contents($urlImagem, false, $context);
这就是我的解决方案。我希望它能帮助其他人。我在使用node.js和npm包(谷歌的官方包)时遇到了同样的问题
虽然我迟到了,但它可能会帮助其他人寻找类似的问题
Node.js
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
//setup your oauth credentials and tokens
oauth2Client.setCredentials(tokens);
var drive = google.drive({
version: 'v2',
auth: oauth2Client
});
drive.files.get({
fileId: fileId, //id of the file you are looking for
alt: 'media'
}, {
responseType: 'arraybuffer',
encoding: null
}, function(err, response) {
if (err) {
console.log(err);
//handle the error
} else {
var imageType = response.headers['content-type'];
var base64 = new Buffer(response.data, 'utf8').toString('base64');
var dataURI = 'data:' + imageType + ';base64,' + base64;
res.send(dataURI);
}
});
HTML
<img src="dataURI_received_from_above_HTTP_request"></img>