Skip to main content
 首页 » 编程设计

python之如何返回 Instagram 实时订阅挑战

2025年05月04日10wuhuacong

我正在尝试订阅标签。似乎使用 hub.challenge 和 hub.mode 正确调用了回调 URL,我想出了如何使用 self.request 访问挑战.get('hub.challenge').我以为我只是应该回应挑战,但这似乎不起作用,因为我在 GAE 日志中收到以下错误:

InstagramAPIError: (400) APISubscriptionError-Challenge verification failed. Sent "647bf6dbed31465093ee970577ce1b72", received " 
647bf6dbed31465093ee970577ce1b72 
". 

这是完整的处理程序:

class InstagramHandler(BaseHandler): 
    def get(self): 
        def process_tag_update(update): 
            update = update 
 
        mode         = self.request.get('hub.mode') 
        challenge    = self.request.get('hub.challenge') 
        verify_token = self.request.get('hub.verify_token') 
        if challenge: 
            template_values = {'challenge':challenge} 
            path = os.path.join(os.path.dirname(__file__), '../templates/instagram.html') 
            html = template.render(path, template_values) 
            self.response.out.write(html) 
        else: 
            reactor = subscriptions.SubscriptionsReactor() 
            reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update) 
 
            x_hub_signature = self.request.headers.get('X-Hub-Signature') 
            raw_response    = self.request.data 
            try: 
                reactor.process('INSTAGRAM_SECRET', raw_response, x_hub_signature) 
            except subscriptions.SubscriptionVerifyError: 
                logging.error('Instagram signature mismatch') 

请您参考如下方法:

所以将它作为字符串返回是可行的。我应该更仔细地注意错误消息,但是 Python IRC 上的一位乐于助人的人指出了消息中额外的换行符。一旦我将模板文件放在一行中,它似乎就可以工作了。我现在可以通过 Instagram 的列表订阅 URL 确认我的应用已获得授权。