简体   繁体   English

为 Facebook Graph API 生成永不过期的令牌,以通过 Google Apps 脚本在 Instagram 商业帐户上发布

[英]Generate non-expiring token for Facebook Graph API to publish on Instagram Business Account via Google Apps Script

To post photos on instagram using Google Apps Script, I follow these steps!要使用 Google Apps 脚本在 Instagram 上发布照片,请按照以下步骤操作!

To generate a short-lived token I access the Graph API Explorer , add these scopes:要生成短期令牌,我访问Graph API Explorer ,添加以下范围:

pages_show_list
instagram_basic
instagram_manage_comments
instagram_manage_insights
instagram_content_publish
pages_read_engagement
pages_manage_posts
public_profile

I click on generate access token, after that I go to the Access Token Debugger , put the generated token, scroll to the bottom of the page and click on extend access token .我单击生成访问令牌,然后转到访问令牌调试器,放置生成的令牌,滚动到页面底部并单击扩展访问令牌

With this long-lived token I add to my code and post images on my Instagram Business Account:使用这个长期存在的令牌,我添加到我的代码中并在我的 Instagram 商业帐户上发布图像:

function instapost(url_photo,txt_subtitle) {
  const access_token = 'XXXXXXXXXXXXX';
  const instagram_business_account = '123456789';
  const image = url_photo;
  const text = txt_subtitle;
  var formData = {
    'image_url': image,
    'caption': text,
    'access_token': access_token
  };
  var options = {
    'method' : 'post',
    'payload' : formData
  };
  const container = 'https://graph.facebook.com/v14.0/' + instagram_business_account + '/media';

  const response = UrlFetchApp.fetch(container, options);

  const creation = response.getContentText();
  var data = JSON.parse(creation);
  var creationId = data.id
  var formDataPublish = {
      'creation_id': creationId,
      'access_token': access_token
  };
  var optionsPublish = {
    'method' : 'post',
    'payload' : formDataPublish
  };
  const sendinstagram = 'https://graph.facebook.com/v14.0/' + instagram_business_account + '/media_publish';
  
  UrlFetchApp.fetch(sendinstagram, optionsPublish);
}

The problem with this is that every 2 months I have to go back and do these manual steps again.问题是每 2 个月我必须返回并再次执行这些手动步骤。

So I tried to generate the short-lived token directly by a direct url call:所以我尝试通过直接 url 调用直接生成短暂的令牌:

https://developers.facebook.com/docs/facebook-login/guides/access-tokens#usertokens https://developers.facebook.com/docs/facebook-login/guides/access-tokens#usertokens

Using this values:使用此值: 在此处输入图像描述

Like this:像这样:

const app_id = ...;
const app_secret = ...;
const url = 'https://graph.facebook.com/oauth/access_token' + 
  '?client_id=' + app_id + 
    '&client_secret=' + app_secret + 
      '&grant_type=client_credentials' +
        '&scope=pages_show_list,instagram_basic,instagram_manage_comments,instagram_manage_insights,instagram_content_publish,pages_read_engagement,pages_manage_posts,public_profile'

UrlFetchApp.fetch(url)

But when going through the debugger, the only existing scope is the public_profile .但是当通过调试器时,唯一存在的范围是public_profile

Is there any way to get this token with all the necessary scopes without having to do it manually?有没有什么方法可以让这个令牌拥有所有必要的范围而无需手动操作?

I've done a lot of research and I always come back to the same problem, because normally the use is aimed at customers, so they will always login and approve access, generating the scopes, but in my case I won't be logging in on the screen, I just want to automate the posts I make on my Instagram.我做了很多研究,我总是回到同样的问题,因为通常使用是针对客户的,所以他们总是会登录并批准访问,生成范围,但在我的情况下,我不会登录在屏幕上,我只想自动化我在 Instagram 上发布的帖子。

Additional information:附加信息:

There are several questions/answears:有几个问题/答案:
https://stackoverflow.com/a/28418469/11462274 https://stackoverflow.com/a/28418469/11462274
https://stackoverflow.com/a/21927690/11462274 https://stackoverflow.com/a/21927690/11462274
https://stackoverflow.com/a/58878246/11462274 https://stackoverflow.com/a/58878246/11462274

About generating an eternal token, in fact there is a way to do it, but the problem is that even though it is eternal, it has a time limit of 2 months for data access, so anyway after 2 months it becomes useless for me, because if he can't access the data, he won't be able to publish.关于生成一个永恒的token,其实是有办法的,但是问题是,虽然是永恒的,但是有2个月的数据访问时间限制,所以反正2个月后对我来说就没用了,因为如果他无法访问数据,他将无法发布。

And as I don't have a company to link to, it becomes unfeasible to try to generate from Meta Business Suite , it's not possible that this is the only way since access to the token is so simple through Graph Explorer.而且由于我没有要链接的公司,因此尝试从Meta Business Suite生成变得不可行,这不可能是唯一的方法,因为通过 Graph Explorer 访问令牌非常简单。

Using https://business.facebook.com/settings/system-users/ , an eternal token is created, but even checking all the scopes released to mark when generating the token, it only returns these authorizations:使用https://business.facebook.com/settings/system-users/ ,创建了一个永恒的令牌,但即使在生成令牌时检查所有释放标记的范围,它也只返回这些授权:

在此处输入图像描述

I'm going to do a complete and definitive step-by-step to solve this problem in relation to posting photos on Instagram with access tokens from the Facebook Graph API.我将一步一步地解决这个问题,这个问题与使用来自 Facebook Graph API 的访问令牌在 Instagram 上发布照片有关。

Assuming that you already have an application created here:假设您已经在此处创建了一个应用程序:
https://developers.facebook.com/apps/ https://developers.facebook.com/apps/

With this products mark:带有此产品标记:

在此处输入图像描述

and linked to your account, access this page:并链接到您的帐户,访问此页面:

https://business.facebook.com/settings/system-users/ https://business.facebook.com/settings/system-users/

There, you will add a new System User but note that there is a very important detail, add it as Administrator Access !在那里,您将添加一个新的系统用户,但请注意有一个非常重要的细节,将其添加为管理员访问权限

After that, you MUST access the Applications tab on the left menu, choose your already created application and click Add People .之后,您必须访问左侧菜单上的Applications选项卡,选择您已经创建的应用程序,然后单击Add People

Select the Admin User you created and check all the existing boxes, which are:选择您创建的管理员用户并选中所有现有的框,它们是:

Develop Application开发应用程序
see insights查看见解
Test Application测试申请
Manage Application管理应用程序

After registering this user with these accesses, go back to the System Users page, click on Generate new token and check all the necessary scopes that are even at the beginning of my question.使用这些访问权限注册此用户后,返回“系统用户”页面,单击“生成新令牌”并检查所有必要的范围,甚至在我的问题开头。

As I already want to prevent future necessary scopes, I marked them all.由于我已经想阻止未来必要的范围,所以我将它们全部标记了。 When debugging the token now here it is, freed forever and with all necessary scopes:现在在这里调试令牌时,它被永久释放并具有所有必要的范围:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM