mirror of
https://github.com/OAuthSwift/OAuthSwift.git
synced 2026-04-26 04:35:56 +03:00
Page:
Instagram
Pages
API with only HTTP scheme into callback URL
App Transport Security
Authenticate with Native Applications
Custom authentication headers
Demo application
How to run Unit Test
Instagram
Interpreting Error Codes
Logout
OAuth 2.0 Token Expiration
OAuth 2.0 Token Revocation
Objective C
Roadmap
Store credential
Uber
Work with application extension
No results
8
Instagram
Bojan Stefanovic edited this page 2018-04-16 22:09:37 -07:00
Table of Contents
- Instagram works only with the http callback URL: API with only HTTP scheme into callback URL
- Using "Client-Side (Implicit) Authentication"
- You must use responseType "token"
- Also, be sure to uncheck the "Disable implicit OAuth" box or else this method will not work.
- The Access token will be accessed from the URL fragment: http://your-redirect-uri#access_token=ACCESS-TOKEN
- Use the internal web web browser technique to access the token. (See the demo app on how to do it in the webview delegate). The server redirect technique won't work as the token is in a url fragment which doesn't make it into the URL when calling a server app.
Using Server-Side Auth Method
Swift
let oAuthRedirectUrl = "http://your_server/the_php_script"
let instagram = OAuth2Swift(
consumerKey: "key",
consumerSecret: "secret",
authorizeUrl: "https://api.instagram.com/oauth/authorize",
responseType: "code" // important! Must be "code" for server-side. Client-side does not work for Instagram due to url fragment
)
...
self.instagram.authorizeURLHandler = SafariURLHandler(viewController: vc, oauthSwift: instagram)
let handle = self.instagram.authorize(
withCallbackURL: URL(string: self.oAuthRedirectUrl)!,
scope: "public_content", state: "INSTAGRAM\(arc4random())",
success: { credential, response, parameters in
// TODO: Save params to UserDefaults
print(parameters)
print("AUTHORIZED INSTAGRAM === \(credential.oauthToken) ")
}, failure: { error in
print(error)
print("OAUTH FAILURE: " + error.localizedDescription)
})
PHP:
<?php
$code = $_GET['code'];
if($code == "")
DIE("ERROR");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://api.instagram.com/oauth/access_token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
client_id => "client_key",
client_secret => "client_secret",
grant_type => "authorization_code",
redirect_uri => "http://your_server/this_script_url_same_as_above",
code => $code
)
));
$resp = curl_exec($curl);
curl_close($curl);
if(!$resp || $resp === ""){
header("Location: your-app-name://oauth-swift/instagram?error=1&error_description=Sorry,%20there%20was%20an%20error");
die;
}
$json = json_decode($resp, true);
$accessToken = $json["access_token"];
$userId = $json["user"]["id"];
$userName = $json["user"]["username"];
header("Location: your-app-name://oauth-swift/instagram?access_token=$accessToken&user_id=$userId&username=$userName");
?>