在我的购物网站中,我实现了“通过 Instagram 登录”的功能,发现它运行良好。现在是否可以将产品图片及其描述分享给用户 Instagram 帐户,或者 Instagram 是否提供任何 javascript 方法,如 facebook、twitter、google+ 等。
简短的回答是:不。发布图像的唯一方法是通过移动应用程序。
来自 Instagram API 文档: http://instagram.com/developer/endpoints/media/
目前无法通过API上传。我们有意识地选择不添加此内容,原因如下:
- Instagram 关乎您的移动生活 – 我们希望鼓励在应用程序内拍摄照片。但是,将来我们可能会根据具体情况授予对各个应用程序的白名单访问权限。
- 我们想要打击垃圾邮件和低质量照片。一旦我们允许从其他来源上传,就很难控制进入 Instagram 生态系统的内容。
尽管如此,我们正在努力确保用户在我们的平台上获得一致且高质量的体验。
可以在 Instagram 上上传。他们的 API 提供了媒体上传端点,即使没有记录。
POST https://instagram.com/api/v1/media/upload/
查看此代码例如https://code.google.com/p/twitubas/source/browse/common/instagram.php
2020 年 6 月更新
据称,这不再可能了。如果您有 Facebook 或 Instagram 专门联系人(因为您在大型机构工作或与大客户合作),根据您的用例,这可能是可能的,但强烈建议不要这样做。
2019 年 12 月之前:
现在“可能”了:
https://developers.facebook.com/docs/instagram-api/content-publishing
内容发布 API 是 Instagram Graph API 端点的子集,允许您发布媒体对象。使用此 API 发布媒体对象分为两个步骤 - 首先创建一个媒体对象容器,然后在您的企业帐户上发布该容器。
值得注意的是,“内容发布 API 仅与 Facebook 营销合作伙伴和 Instagram 合作伙伴进行内测。我们目前不接受新的申请者。”来自https://stackoverflow.com/a/49677468/445887
Instagram 最近发布了其原生分享功能的更新。仅可通过移动浏览器使用
navigator.share
,MDN 参考此处。
建议的实现是检查
navigator.canShare
,然后使用 share
原生共享 url
、text
或 files
。对于 Instagram,files
将最有用,正如您在下面的示例中看到的,您可以共享网络上的图像。这是完成此操作的示例代码。
const shareImageAsset = async (): Promise<boolean> => {
const filesArray = [
new File([blobImageAsset], `${title}.png`, {
type: 'image/png',
lastModified: new Date().getTime(),
}),
];
const shareData = {
title: `${title}`,
files: filesArray,
};
if (navigator.canShare && navigator.canShare(shareData)) {
await navigator.share(shareData);
}
};
注意:
blogImageAsset
最初是从远程获取的png
图像url
并转换为blob
,如下所示:
const response = await fetch(url.toString());
const blob = await response.blob();
保持标题、文件类型和原始图像类型对齐非常重要,否则将会失败。
最终看起来像这样:
扩展bcaspar的答案,使用
navigator.share
的解决方案,这应该是您可以尝试的简单的可运行代码:
const shareImageAsset = async () => {
try {
const url = 'https://via.placeholder.com/1080x1920';
const response = await fetch(url, {
mode: "no-cors", // might not be needed if image is from same domain
});
const blobImageAsset = await response.blob();
const filesArray = [
new File(
[blobImageAsset],
`file.png`, // type in the file name must be the same as the image
{
type: blobImageAsset.type,
}
),
];
const shareData = {
files: filesArray,
};
if (navigator.canShare && navigator.canShare(shareData)) {
await navigator.share(shareData);
} else {
throw new Error("navigator.share is not available");
}
} catch(err) {
document.getElementById('output').textContent += `${err.name}: ${err.message}\n`;
}
};
document.getElementById('button').addEventListener('click', shareImageAsset);
<button id="button">Share Image</button>
<div id="output"></div>
自2015年11月17日起,本规则已正式变更。 Instagram 已弃用禁止使用其 API 上传图像的规则。
祝你好运。