Search code examples
iostwitter-oauthtwitter-fabric

iOS: give server twitter access?


I'd like to let the user choose a twitter account and authorize the app, using Accounts.framework (and hopefully just Fabric) to do something. But then, I want my server to actually do the thing: perform the twitter call (get all people the user follows and, I dunno, count them).

Is there any way? I assume it's not as easy as passing the authToken I get back from Fabric's login to my server...


Solution

  • You are looking for OAuth Echo: https://docs.fabric.io/ios/twitter/oauth-echo.html

    You generate the oauth headers needed for your server to make the request, for the specific endpoint:

    TWTROAuthSigning *oauthSigning = [[TWTROAuthSigning alloc] initWithAuthConfig:[Twitter sharedInstance].authConfig authSession:[Twitter sharedInstance].session];
    NSString *endpoint = @"https://api.twitter.com/1.1/friends/ids.json";
    NSDictionary *parameters = @{
                                 @"cursor": @"-1",
                                 @"screen_name": username,
                                 @"count": @"2000"
                                 };
    NSError *error;
    NSDictionary *oauthHeaders = [oauthSigning OAuthEchoHeadersForRequestMethod:@"GET" URLString:endpoint parameters:parameters error:&error];
    

    A key in oauthHeaders is "X-Verify-Credentials-Authorization" which you need to set as your Authorization header in the request you make from your server. The other key is "X-Auth-Service-Provider" which is the url you should make your request to (the endpoint you specified) - it's the only endpoint the headers will work at, and it includes a query string of the GET params you pass to OAuthEchoHeadersForRequestMethod.

    The method OAuthEchoHeadersForRequestMethod isn't very well documented... found it thanks to autocomplete suggestions.