简体   繁体   中英

Trying to get access token for Twitter using Firebase Twitter authentication in ios

This is the code used to login to the twitter account. I am able to login and get redirected to the app but I am not able to retrieve a proper access token(Bearer token as it says on the twitter api page). I get an AuthDataResult which contains a access token but when used to make an api call it does not work.

final class AuthManager{


var isSignedIn:Bool{
    return firebase.currentUser != nil
}

static let shared = AuthManager()
var provider:OAuthProvider? 
let firebase = Auth.auth()

public func handleAuthentication(){
    provider = OAuthProvider(providerID: "twitter.com")
    provider?.getCredentialWith(nil) { (credential, err) in
        if err != nil {
            
        }
       
            guard let cred  = credential else {return}

            self.firebase.signIn(with: cred) { (data, err) in
                
                if err != nil {
                    print(err?.localizedDescription as Any)
                }
                
                if data != nil {
                    let newCred:OAuthCredential = data?.credential as! OAuthCredential

                    print(newCred.accessToken)
                    print(newCred.idToken) //-> nil
                    print(newCred.secret)
                

                
                }
                
            }
        
    }
    

I used the access token to make an api call but I received an error saying unauthorized call.

func getTweet(accessToken:String){
        guard let apiUrl = URL(string: "https://api.twitter.com/2/tweets/1261326399320715264") else {return}
        var request = URLRequest(url: apiUrl)
        request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
        request.httpMethod = "GET"
        request.timeoutInterval = 30
        
        let task = URLSession.shared.dataTask(with: request) { (data, res, err) in
            
            guard let data  = data else {return}
            do{
                let resutl = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                print(resutl)
            }
            catch{
                print(error.localizedDescription)
            }
        }
        
        task.resume()
    }

Bearer tokens are different to user access tokens. A Bearer Token in the current versions of the Twitter API is used to authenticate an application context, and is generated differently to the user access token. Firebase login is typically used to obtain a user access token, which authenticates a user. You cannot use the values interchangeably, which is why you get an authentication error trying to call the API.

You can obtain a bearer token by passing the consumer key and secret (aka API key and secret) to the Twitter API like so (example using curl)

curl -u "$CONSUMER_KEY:$CONSUMER_SECRET" --data 'grant_type=client_credentials' 'https://api.twitter.com/oauth2/token'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM