Last active
July 23, 2019 14:53
-
-
Save plateaukao/4eb465b48e4f996dd97d566aa25b8383 to your computer and use it in GitHub Desktop.
oauth1_init.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:oauth1/oauth1.dart' as oauth1; | |
const String FLICKR_API_URL_OAUTH_BASE="https://www.flickr.com/services/oauth"; | |
const String FLICKR_API_URL_REQUEST_TOKEN ="$FLICKR_API_URL_OAUTH_BASE/request_token"; | |
const String FLICKR_API_URL_AUTHORIZE="$FLICKR_API_URL_OAUTH_BASE/authorize"; | |
const String FLICKR_API_URL_ACCESS_TOKEN="$FLICKR_API_URL_OAUTH_BASE/access_token"; | |
class FlickrApiClient { | |
final platform = new oauth1.Platform( | |
FLICKR_API_URL_REQUEST_TOKEN, | |
FLICKR_API_URL_AUTHORIZE, | |
FLICKR_API_URL_ACCESS_TOKEN, | |
oauth1.SignatureMethods.hmacSha1// signature method | |
); | |
final clientCredentials = new oauth1.ClientCredentials(FLICKR_API_KEY, FLICKR_API_SECRET); | |
var _auth = new oauth1.Authorization(clientCredentials, platform); | |
oauth1.Credentials _tempCredentials; | |
// get temp token from flickr and return web url | |
Future<String> getRequestTokenUrl() async { | |
final response = await _auth.requestTemporaryCredentials('oob'); | |
_tempCredentials = response.credentials; | |
return "${_auth.getResourceOwnerAuthorizationURI(_tempCredentials.token)}"; | |
} | |
// get real token with pin code input from user | |
Future<oauth1.Client> requestToken(String verifier) async { | |
final response = await _auth.requestTokenCredentials(_tempCredentials, verifier); | |
_tempCredentials = null; | |
// save the token to local storage | |
_saveToken(response.credentials); | |
// this is the oauth1 client that we can use as httpClient to call other APIs that needed authentication. | |
authClient = oauth1.Client(platform.signatureMethod, clientCredentials, response.credentials); | |
return authClient; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment