-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Medium links do not follow redirect #43
Comments
@peterfriese |
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. |
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)
The text was updated successfully, but these errors were encountered: