Git Product home page Git Product logo

Comments (13)

s-aska avatar s-aska commented on May 18, 2024 1

Access token

// save session
let accessToken = swifter.client.credential.accessToken {
    let key = accessToken.key // String
    let secret = accessToken.secret // String
    // save the key and secret to Keychain
}

// load session
let key = ... // load the key from Keychain
let secret = ... // load the secret from Keychain
let accessToken = SwifterCredential.OAuthAccessToken(key: key, secret: secret)
let credential = SwifterCredential(accessToken: accessToken)

let swifter = Swifter(consumerKey: "", consumerSecret: "")
swifter.client.credential = credential

ACAccount

// save session
if let account = swifter.client.credential.account {
    let identifier = account.identifier // String
    // save identifier to Keychain
}

// load session
let identifier = ... // load the identifier by Keychain
let account = ACAccountStore().accountWithIdentifier(identifier)
let credential = SwifterCredential(account: account)

let swifter = Swifter(consumerKey: "", consumerSecret: "")
swifter.client.credential = credential

from swifter.

iRILLLL avatar iRILLLL commented on May 18, 2024

Hi, thanks for your time to answer my question.

I've got an error when I tried to load the session

HTTP Status 400: Bad Request, Response: {"errors":[{"message":"Bad Authentication data","code":215}]}

if I change the codes when I load the session to this

let swifter = Swifter(consumerKey: myConsumerKey, consumerSecret: myConsumerSecret)
swifter.client.credential = credential

I got this error

HTTP Status 401: Unauthorized, Response: {"errors":[{"message":"Could not authenticate you","code":32}]}

do you know where is the problem?

from swifter.

s-aska avatar s-aska commented on May 18, 2024

umm.
there is a possibility that you tweet without setting the write permissions.

https://apps.twitter.com/app/YOUR_APP_ID/permissions

Note: https://dev.twitter.com/overview/api/response-codes

from swifter.

jchesne avatar jchesne commented on May 18, 2024

Hello s-aska,

I have tried to update twitter session with your code, here is mine :

        let key = "my-twitter-from-keychain"
        let secret = "my-twitter-secret-from-keychain"
        let accessToken = SwifterCredential.OAuthAccessToken(key: key!, secret: secret!)
        let credential = SwifterCredential(accessToken: accessToken)

         self.swifter = Swifter(consumerKey: "my-key", consumerSecret: "my-secret")
        swifter.client.credential = credential

But this seem not working properly, like if not call to twitter were done.
Here is my credential :

swifter-help

As you can see, data are nil, can you tell me have i don't do properly please ?

Thanks,

from swifter.

alexdd55 avatar alexdd55 commented on May 18, 2024

i did this to solve this problem:

if userHelper.fetchUserToken() != nil {
            self.swifter.client.credential = userHelper.fetchUserToken()
            user = self.userHelper.fetchUserQData()
        } else {
            self.swifter.authorizeWithCallbackURL(NSURL(string: "some.twitterapp://success")!, success: {
                credential, response in
                var tmp = self.swifter.client.credential?.accessToken?
                self.userHelper.saveUserToken(tmp!)

                },failure: failureHandler
            )
        }

func saveUserToken(data: SwifterCredential.OAuthAccessToken) -> Bool {

        var userdata = NSUserDefaults.standardUserDefaults()
        userdata.setObject(data.key, forKey: "key")
        userdata.setObject(data.secret, forKey: "secret")
        userdata.setObject(data.screenName, forKey: "screenName")
        userdata.setObject(data.userID, forKey: "userID")
        userdata.setObject(data.verifier, forKey: "verifier")
        userdata.synchronize()

        return true
    }

    func fetchUserToken() -> SwifterCredential? {
        var userdata = NSUserDefaults.standardUserDefaults()

        if var tkey = userdata.objectForKey("key") as? String {
            if var tsecret = userdata.objectForKey("secret") as? String {

                var access = SwifterCredential.OAuthAccessToken(key: tkey , secret: tsecret)
                return SwifterCredential(accessToken: access)
            }
        }
        return nil
    }

    func fetchUserQData() -> NSDictionary {
        var userdata = NSUserDefaults.standardUserDefaults()
        var data: NSMutableDictionary = NSMutableDictionary()
        data["screenName"] = userdata.objectForKey("screenName") as String?
        data["userID"] = userdata.objectForKey("userID") as String?
        return data
    }

i am sure, there are better ways to do that ;)

from swifter.

jchesne avatar jchesne commented on May 18, 2024

Hi Alex,

My question was not about how to save usertoken.

My problem is when i restart my app, how do i ask to twitter/swifter to refresh the token with my saved usertoken for dont have to login again anymore ?

Do you succeed to do this ?

from swifter.

alexdd55 avatar alexdd55 commented on May 18, 2024

i updated my answer above.

acaccount will stay nil.
i didn't get along with it,..

the others nil values are coming with the credentials i saved. so you can use these.

after authentication (twitter webpage) there is now need to login again, until you uninstall the app from your phone or simulator.

from swifter.

jchesne avatar jchesne commented on May 18, 2024

Ok Alex, and can you confirm that when you debug the user variable at this line :
user = self.userHelper.fetchUserQData()
You fetch exactly the same data as authorizeWithCallbackURL do please with your tmp var ?

As you can see in my debug picture, only the key and secret have data,

from swifter.

alexdd55 avatar alexdd55 commented on May 18, 2024

i had to add another piece of code. ;)

actually, you could just copy and paste the code. it will do a couple of steps to get the data and save it for all coming requests, incl authentication call if neccessary.

from swifter.

jchesne avatar jchesne commented on May 18, 2024

Thanks for your code ;)

So you confirm that :

var access = SwifterCredential.OAuthAccessToken(key: tkey , secret: tsecret)
return SwifterCredential(accessToken: access)

Does'nt return the fully object whith screenName and userID ? Its not normal isn't it ?

from swifter.

alexdd55 avatar alexdd55 commented on May 18, 2024

i do not really know if its normal - to be honest i expected this data, too ;) - but during debugging it shows, i need to do a request to get it....

from swifter.

jchesne avatar jchesne commented on May 18, 2024

Ok, i will continu to investigate. Thanks for you help !

from swifter.

zntfdr avatar zntfdr commented on May 18, 2024

It's been over an year since the last update of this issue: I believe the school's project is done by now :)

from swifter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.