Closed peterfriese closed 4 years ago
@peterfriese Thanks for your information. But I couldn't reproduce the redirection behavior and suppose that URLSession normally follow redirects automatically. I'll be able to investigate the problem if you provide the iOS version and OpenGraph version that the problem occurred.
When trying to fetch Open Graph data from a Medium link, Medium will send an interstitial screen which is trying to convince the user to open the respective article in their native app.
I worked around this behaviour by sending a desktop user agent when fetching the Open Graph data. Here is the code I use:
func fetchLinkMetadata(url: URL) {
self.logger.debug("Fetching meta data using Open Graph")
// This header makes sure we request the desktop website, which will prevent Medium from trying to display a "open this in the app" interstitial
let headers = ["User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"]
OpenGraph.fetch(url: url, headers: headers ) { result in
switch result {
case .success(let og):
if let finalUrl = og[.url] {
self.metaUrl = finalUrl
}
if let title = og[.title] {
self.metaTitle = title
DispatchQueue.main.async {
if self.textView.text.isEmpty || self.textView.text == url.absoluteString {
self.logger.debug("Updating UI title: \(title)")
self.textView.text = title
}
}
}
if let siteName = og[.siteName] {
self.siteName = siteName
}
if let description = og[.description] {
self.metaDescription = description
}
if let author = og[.bookAuthor] {
self.metaAuthor = author
}
case .failure(let error):
print(error)
}
}
}
I've seen other OG frameworks that handle this behind the scenes. It might be worth while to:
I found that OpenGraph hadn't handle the redirect since the interstitial screen provokes the redirection on their front-end JavaScript codes. Also, I think it's better that this OpenGraph library doesn't handle front-end redirection so that it keeps the isolation from any presentation logic. Thus I'll update README to let OG users know how to handle these kind of situations without any code changes.
When trying to parse OG meta data from Medium articles shared from within their iOS app, OpenGraph returns an empty result.
URLs shared form their app look like this:
https://link.medium.com/oT1YJfn1G9
Retrieving this URL with curl resolves to the final URL (https://onezero.medium.com/i-bought-a-new-router-it-told-me-i-was-hacked-fb141930dd22?source=userActivityShare-ea0b1eb1f5d2-1599825776&_branch_match_id=link-832936551787943816)