0

我正在尝试通过 instagram Graph API 从 instagram 获取数据。为此,我使用了一个带有名为 limit 的参数的 url,它返回一个 Json。限制将始终超过 1,并且可以达到 12。限制代表您想要获得的最后帖子的数量。

此限制必须等于用户在其 Instagram 上发布的帖子数量。如果您要求的限制低于此限制,您将不会收到所有帖子。如果结束,则 Json 响应为错误。

错误Json:

{
   "error": {
      "message": "Invalid parameter",
      "type": "OAuthException",
      "code": 100,
      "error_data": {
         "blame_field_specs": [
            [
               ""
            ]
         ]
      },
      "error_subcode": 2108006,
      "is_transient": false,
      "error_user_title": "Media Posted Before Business Account Conversion",
      "error_user_msg": "The media was posted before the most recent time that the user's account was converted to a business account from a personal account."
   }
}

到目前为止,我的想法是尝试 12 个 url,计算其中有多少是有效的并返回正确的限制。它一直运行良好,直到最近我发现 API 在 100% 的情况下都无法返回错误。

这是我的代码:

//Find url limit
extension GetJson {
    class func findMediaLimit(IgBusinessAccount: String, token: String, Completion block: @escaping ((Int) -> ())) {
        
        let igBId = IgBusinessAccount
        let group = DispatchGroup()
        var mCount: [Int] = []
    
        
        for i in 1...12 {
            
            guard let encodedUrl = self.buildURLAPIGraph(IgBusinessAccount: igBId, token: token, i: i) else { return }
            
            
            group.enter()
            self.cURL(urlT: encodedUrl) { (Json) in
                
                if Json.username != nil {
                    print("\n\nR with \(i):")
                    print(Json.media ?? "no")
                    print("\n\n")
                    print(encodedUrl)
                    print("\n\n")
                    mCount.append(1)
                }
                
                group.leave()
            }
        }
        
        group.notify(queue: .main) {
            print("Media limit: \(mCount.count)")
            block(mCount.count)
        }
    }
}

在这里,我正在测试 Json 是否对“Json.username != nil”有效,但我发现即使限制错误,它有时也会进入条件,因为 API 有时无法返回错误。

因此,这不是避免“在商业帐户转换之前发布媒体”的最佳方法

理想情况下,从 API 中知道这个限制会很棒,但我认为这个功能不存在。否则有人知道我该怎么做吗?

4

1 回答 1

0

我的解决方法:

//Find url limit
extension GetJson {
    class func findMediaLimit(IgBusinessAccount: String, token: String, Completion block: @escaping ((Int) -> ())) {
        
        
        let igBId = IgBusinessAccount
        var mCount: [Int] = []
    
        let group = DispatchGroup()
        
        for i in 1...12 {
            
            //Test 12 urls to find the limit
            guard let encodedUrl = self.buildURLAPIGraph(IgBusinessAccount: igBId, token: token, i: i) else { return }
          
            group.enter()
            
            self.cURL(urlT: encodedUrl) { (Json) in
                
                if Json.username != nil { //means no error returned
                    
                    /*Hack: Api sometimes fail to return an error and returns a json,
                    But the Json media's count is actually the limit to find*/
                    mCount.append(Json.media?.data.count ?? 0)
                }
                
                group.leave()
            }
        }
        
        group.notify(queue: .main) {
            let limit = mCount.max() ?? 0
            
            //print("Media limit: \(mCount.count)") old
            print("Media limit: \(limit)")
            
            block(limit)
        }
    }
}
于 2021-07-09T15:13:52.390 回答