I am trying to get data from instagram through the instagram Graph API. For that, I use a url with a parameter called limit, which returns a Json. Limit will always be over 1, and can go up to 12. Limit is representative of how many of the last posts you want to get.
This limit must be equal to the number of posts the user have posted on his/her Instagram. If you ask a limit that is below, you won't get all the posts. If it's over, the Json response is an error.
The error 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."
}
}
So far my idea was to try 12 urls, count how many of them were valid and return the right limit. It was working well until recently, when I found out the API could fail to return an error in 100% of cases.
this is my code:
//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)
}
}
}
Here I'm testing if the Json is valid with "Json.username != nil" But I found out that even with a wrong limit it sometimes enters the condition because the API sometimes fails to return an error.
So this isn't the best way to avoid "Media Posted Before Business Account Conversion"
Idealy knowing this limit from the API would be great but I don't think this feature exists. Otherwise anybody have an idea how I could do?