[GH-ISSUE #26] Keep user logged #17

Closed
opened 2026-03-03 16:44:49 +03:00 by kerem · 4 comments
Owner

Originally created by @pistacchio on GitHub (Feb 20, 2015).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/26

Hi,
suppose that I have an app with a Twitter login button. After the login, I'm given back the credentials and I can store them in NSUserDefaults.standardUserDefaults()
If the user runs the app again, and I see that he has auth credentials already set, how can I set up the oauth client so that it uses them without requiring the login again?

Also, once the user it logged in, how to get info of the logged user, such is user id etc?

Thanks

Originally created by @pistacchio on GitHub (Feb 20, 2015). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/26 Hi, suppose that I have an app with a Twitter login button. After the login, I'm given back the credentials and I can store them in NSUserDefaults.standardUserDefaults() If the user runs the app again, and I see that he has auth credentials already set, how can I set up the oauth client so that it uses them without requiring the login again? Also, once the user it logged in, how to get info of the logged user, such is user id etc? Thanks
kerem closed this issue 2026-03-03 16:44:50 +03:00
Author
Owner

@Niels-P commented on GitHub (Mar 1, 2015):

Okay I found out how to do it.

In the framework file OAuthClientSwift you add this function in the first class

    public func setUserDetails(token: String, secret: String, parameters: Dictionary<String, AnyObject>) {
        self.credential.oauth_token_secret = secret;
        self.credential.oauth_token = token;
    }

Then in your view controller you add:

oauthswift.client.setUserDetails("Here the token you saved", secret: "here the secret you saved", parameters: parameters)

        oauthswift.client.get("https://api.linkedin.com/v1/people/~:(skills,first-name,last-name,picture-url)", parameters: parameters,
            success: {
                data, response in
                println("PARAMETERS: \(parameters)");
                let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
                println(parameters);
                println(dataString);
            }, failure: {(error:NSError!) -> Void in
                println(error)
        })

That way you can use the token and secret without having to authorize all over again. Remember to check if the token is still valid by checking the LinkedIn response on failure.

<!-- gh-comment-id:76625772 --> @Niels-P commented on GitHub (Mar 1, 2015): Okay I found out how to do it. In the framework file OAuthClientSwift you add this function in the first class ``` public func setUserDetails(token: String, secret: String, parameters: Dictionary<String, AnyObject>) { self.credential.oauth_token_secret = secret; self.credential.oauth_token = token; } ``` Then in your view controller you add: ``` oauthswift.client.setUserDetails("Here the token you saved", secret: "here the secret you saved", parameters: parameters) oauthswift.client.get("https://api.linkedin.com/v1/people/~:(skills,first-name,last-name,picture-url)", parameters: parameters, success: { data, response in println("PARAMETERS: \(parameters)"); let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) println(parameters); println(dataString); }, failure: {(error:NSError!) -> Void in println(error) }) ``` That way you can use the token and secret without having to authorize all over again. Remember to check if the token is still valid by checking the LinkedIn response on failure.
Author
Owner

@pistacchio commented on GitHub (Mar 2, 2015):

Hi,
yes, I ended up doing something very similar to be able to set the credentials from outside the class. I think this should be added to the project.

<!-- gh-comment-id:76673510 --> @pistacchio commented on GitHub (Mar 2, 2015): Hi, yes, I ended up doing something very similar to be able to set the credentials from outside the class. I think this should be added to the project.
Author
Owner

@hopye commented on GitHub (Apr 16, 2015):

i have the same questions, im using instagram auth . i do not know how to get the id, and other info instagram provides when authenticating... i took the code from the example...

Instagram only provides that info at authorization only looks like, then you can use the other end points, but theres no https://api.instagram.com/v1/users/myinfo , the /self/feed , returns the feed from the user but no logged user info..

Can someone please point me in the right direction ?

oauthswift.authorizeWithCallbackURL( NSURL(string: "comalacartado://oauth-callback/instagram")!, scope: "likes+comments+basic", state:"INSTAGRAM", success: {
credential, response in
self.showAlertView("Instagram", message: "oauth_token:(credential.oauth_token)")

        let url :String = "https://api.instagram.com/v1/users/self/feed/?access_token=\(credential.oauth_token)"
        let parameters :Dictionary = Dictionary<String, AnyObject>()
<!-- gh-comment-id:93605440 --> @hopye commented on GitHub (Apr 16, 2015): i have the same questions, im using instagram auth . i do not know how to get the id, and other info instagram provides when authenticating... i took the code from the example... Instagram only provides that info at authorization only looks like, then you can use the other end points, but theres no https://api.instagram.com/v1/users/myinfo , the /self/feed , returns the feed from the user but no logged user info.. Can someone please point me in the right direction ? oauthswift.authorizeWithCallbackURL( NSURL(string: "comalacartado://oauth-callback/instagram")!, scope: "likes+comments+basic", state:"INSTAGRAM", success: { credential, response in self.showAlertView("Instagram", message: "oauth_token:(credential.oauth_token)") ``` let url :String = "https://api.instagram.com/v1/users/self/feed/?access_token=\(credential.oauth_token)" let parameters :Dictionary = Dictionary<String, AnyObject>() ```
Author
Owner

@phimage commented on GitHub (Jun 26, 2015):

If PR #78 validated

oauthswift.client.credential.oauth_token = "here your data"
oauthswift.client.credential.oauth_token_secret = "here your secret data"
<!-- gh-comment-id:115567468 --> @phimage commented on GitHub (Jun 26, 2015): If PR #78 validated ``` swift oauthswift.client.credential.oauth_token = "here your data" oauthswift.client.credential.oauth_token_secret = "here your secret data" ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#17
No description provided.