iOS: get Twitter feed of public account - ios

I need to get a Twitter feed of a public account. I am able to get my own Twitter feed using the following code:
-(void) testTwitter {
{
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
if (accountStore != nil)
{
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if (accountType != nil)
{
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
//Succesful Access
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
if (twitterAccounts != nil)
{
ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
if (currentAccount != nil)
{
NSString *paraString = [NSString stringWithFormat:#"qlx_corp"];
NSDictionary *parameters = [NSDictionary dictionaryWithObject:paraString forKey:#"username"];
NSString *requestString = #"https://api.twitter.com/1.1/statuses/user_timeline.json";
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString] parameters:parameters];
[request setAccount:currentAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ((error == nil) && ([urlResponse statusCode] == 200))
{
NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSDictionary *firstPost = [twitterFeed objectAtIndex:0];
NSLog(#"firstPost = %#", [firstPost description]);
}
else {
NSLog(#"error: %#",error);
}
}];
}
}
}
else
{
//Access Denied
NSLog(#"access denied");
}
}];
}
}
}
}
But when I substitute the following code for getting another account besides my own, I get this error:
code = 215;
message = "Bad Authentication data.";
And this is the code I substituted for a Successful Access (noted in the comments in posted code):
NSURL* url = [NSURL URLWithString:#"https://api.twitter.com/1.1/statuses/user_timeline.json"];
NSDictionary* params = #{#"count" : #"10", #"screen_name" : #"qlx_corp"};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:url parameters:params];
[request performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
NSError *jsonError;
NSArray *responseJSON = [NSJSONSerialization
JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSLog(#"response json: %#",responseJSON);
// Show the content of the responseJSON in a view.
}
}];
What am I doing wrong that I am getting this error message and not the public twitter feed that I am requesting. My app asks for permission for Twitter use and it has been granted, so what could be the problem?

I ended up using the Twitter API, since now they support an application only mode that does not require user authentication to display a public Twitter feed. Refer to this link for some more information: Twitter OAuth and accessToken to GET statuses/user_timeline in Objective-C

Related

How to get Email param of facebook using SLRequest in iOS Social Framework

I tried the below code for getting email of the person who logged in iOS Settings Facebook.
Please help me how to get email from SLRequest.
- (void) getMyDetails {
if (! _accountStore) {
_accountStore = [[ACAccountStore alloc] init];
}
if (! _facebookAccountType) {
_facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
}
NSDictionary *options = #{ ACFacebookAppIdKey: FB_APP_ID };
[_accountStore requestAccessToAccountsWithType: _facebookAccountType
options: options
completion: ^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [_accountStore accountsWithAccountType:_facebookAccountType];
_facebookAccount = [accounts lastObject];
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:url
parameters:nil];
request.account = _facebookAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:nil];
NSLog(#"id: %#", responseDictionary[#"id"]);
}];
}
}];
}
Here is the code tested on iOS 8 that returns email. The solution doesn't require Facebook SDK, although the system grants access to Facebook account only if a user logged in with Facebook in Settings app.
// Required includes
#import Accounts;
#import Social;
// Getting email
ACAccountStore *theStore = [ACAccountStore new];
ACAccountType *theFBAccountType = [theStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *theOptions = #{
ACFacebookAppIdKey : #"YOUR_APP_ID",
ACFacebookPermissionsKey : #[#"email"]
};
[theStore requestAccessToAccountsWithType:theFBAccountType options:theOptions completion:^(BOOL granted, NSError *error) {
if (granted) {
ACAccount *theFBAccount = [theStore accountsWithAccountType:theFBAccountType].lastObject;
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:#"https://graph.facebook.com/me"]
parameters:#{#"fields" : #[#"email"]}];
request.account = theFBAccount;
[request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil && ((NSHTTPURLResponse *)response).statusCode == 200) {
NSError *deserializationError;
NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&deserializationError];
if (userData != nil && deserializationError == nil) {
NSString *email = userData[#"email"];
NSLog(#"%#", email);
}
}
}];
}
}];
You can get email id by below mentioned way.
Call Facebook graph API by the help of access token that you got from Account store
And yes, to get email id from Facebook, you need to provide "email" permission while requesting access token, without the permission you won't be able to get email parameter
Here is my code
NSString *FB_EncodedToken = [APP_CONSTANT.facebookToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *opearation = [AFHTTPRequestOperationManager manager];
opearation.requestSerializer = [AFHTTPRequestSerializer serializer];
opearation.responseSerializer = [AFJSONResponseSerializer serializer];
NSString *strUrl = [NSString stringWithFormat:#"https://graph.facebook.com/me?"];
NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:FB_EncodedToken,#"access_token", nil];
[opearation GET:strUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
DLogs(#"Description %#",responseObject);
//Lets pasre the JSON data fetched from facebook
[self parseUserDetail:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLogs(#"Error description %#",error.description);
self.completionHandler(error);
}];
Then parse the data
-(void)parseUserDetail:(NSDictionary *)dict
{
FBProfileBO *profile = [[FBProfileBO alloc] init];
profile.userFirstName = [dict objectForKey:#"first_name"];
profile.userLastName = [dict objectForKey:#"last_name"];
profile.userEmail = [dict objectForKey:#"email"];
profile.userName = [dict objectForKey:#"name"];
profile.userDOB = [dict objectForKey:#""];
profile.facebookId = [dict objectForKey:#"id"];
//Call back methods
self.completionHandler(profile);
profile = nil;
}

Twitter share with SLRequest

This is Sam.Currently I am devloping twitter share in my application.I need to implement that without ComposeController.I need to integrate with SLRequest from social framework.
The Code which i using is as below as
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType =[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(#"Creating Request");
// Step 2: Create a request
NSArray *twitterAccounts =[accountStore accountsWithAccountType:twitterAccountType];
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1.0/statuses/update.json"];
NSDictionary *params = #{#"screen_name" : #"naheshsamy",#"include_rts" : #"0",#"trim_user" : #"1",#"count" : #"1"};
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params];
// Attach an account to the request
[request setAccount:[twitterAccounts lastObject]];
NSLog(#"Request %#",request);
// Step 3: Execute the request
[request performRequestWithHandler: ^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error) {
if (responseData) {
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
NSError *jsonError;
NSDictionary *timelineData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];
if (timelineData) {
NSLog(#"Timeline Response: %#\n", timelineData);
}
else {
// Our JSON deserialization went awry
NSLog(#"JSON Error: %#", [jsonError localizedDescription]);
}
}
else {
// The server did not respond ... were we rate-limited?
NSLog(#"The response status code is %d %# %#",urlResponse.statusCode,urlResponse.suggestedFilename,error);
}
}
}];
}
else {
// Access was not granted, or an error occurred
NSLog(#"%#", [error localizedDescription]);
}
}];
I am always getting status 400 error..
Any help will be appreciated.
Regards,
Sam.P
The code seems similar to what I have used.
They do have warned that version 1.0 of the API is no longer supported and may not function. Try changing to 1.1 and see if it solves your problem.
#"https://api.twitter.com/1.1/statuses/update.json"

Error Code 220 - "Your credentials do not allow access to this resource" When tryig to get retweets

When i try to get https://api.twitter.com/1.1/statuses/retweets/21947795900469248.json with my authorized credentials (oauth), i'm getting:
{
"errors": [
{
"message": "Your credentials do not allow access to this resource",
"code": 220
}
]
}
error: Any other twit id's not working. Is there a problem with this endpoint? Because i was getting true response until yesterday. Something changed?
I experienced same issue with getting error code 220. Fix in my case was to prompt user to enter it's Twitter password again.
Check sample code below:
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://api.twitter.com/1.1/statuses/update.json"]
parameters:#{#"status": tweetString}];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if ([responseData isKindOfClass:[NSData class]]) {
NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];
if (json) {
NSArray *errors = json[#"errors"];
if (errors.count) {
NSUInteger errorCode = [json[#"errors"][0][#"code"] integerValue];
if (errorCode == 220) {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
[accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error2) {
NSLog(#"Renew result: %ld, error: %#", (long)renewResult, error2);
if (renewResult == ACAccountCredentialRenewResultRenewed) {
[self __makeTweetWithAccounts:#[account] rating:rating review:review];
}
}];
}
}
}
}
NSLog(#"Twitter response data: %#", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
Was getting the same error when I noticed I was using GET instead of POST. Changed it and it worked.
Set app permissions to Read, write, and direct messages
...and Regenerate Access Token and Token Secret.
It worked for me
I had similar problem with SLRequest class on iOS SDK7.1. Now to get retweets, or post retweet, i started to use deprecated class TWRequest. Just #import resource and use this method:
- (void)retweetMessageV2HavingId:(NSString*)messageId {
NSString *retweetString = [NSString stringWithFormat:#"https://api.twitter.com/1.1/statuses/retweet/%#.json", messageId];
NSURL *retweetURL = [NSURL URLWithString:retweetString];
TWRequest *request = [[TWRequest alloc] initWithURL:retweetURL parameters:nil requestMethod:TWRequestMethodPOST];
request.account = self.account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData)
{
NSError *parseError = nil;
id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
if (!json)
{
NSLog(#"Parse Error: %#", parseError);
}
else
{
NSLog(#"%#", json);
}
}
else
{
NSLog(#"Request Error: %#", [error localizedDescription]);
}
}]; }
I have generated access-tokens going to 'Keys and Access Tokens' tab in my current app page. Then, i have set 'Access Token' and 'Access Token Secret' for the access tokens for 'auth' obj that has permisson for the app to access in source code.

Getting email using ACAccount for Facebook iOS 7

First of all, I don't want to use Facebook SDK, so I managed to do this, which is working, but I don't know how to retrieve user's email, even though it is on my permissions request.
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:accountTypeName];
NSDictionary *options;
if ( [accountTypeName isEqualToString:ACAccountTypeIdentifierFacebook]){
options = #{
ACFacebookAppIdKey: #"601517946570890"
,ACFacebookPermissionsKey: #[#"email"]
};
}
[account requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
_account = [arrayOfAccounts lastObject];
NSDictionary *dict = #{
#"name": [_account userFullName] == nil ? [_account username] : [_account userFullName],
#"account_id": [_account identifier],
#"email": #"??"
};
NSLog(#"account info: %#",dict);
}
}
}];
there is no property on ACAccount that returns user e-mail, so I tried to find if it had to be done via SLRequest and couldn't find it.
Is it possible?
In order to get the user's email address from Facebook, you have to use the Graph API to query the current user using the Social framework:
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:#"https://graph.facebook.com/me"]
parameters:nil];
request.account = _account; // This is the _account from your code
[request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil && ((NSHTTPURLResponse *)response).statusCode == 200) {
NSError *deserializationError;
NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&deserializationError];
if (userData != nil && deserializationError == nil) {
NSString *email = userData[#"email"];
NSLog(#"%#", email);
}
}
}];
Now you need to pass param in GET request. by default only ID & name.
NSDictionary *param = #{#"fields":#"email,name"};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:#"https://graph.facebook.com/me"]
parameters:param];

Twitter error - verify_credentials request fails but tweeting and other endpoints still accessible

I'm trying to access the user's credentials of Twitter with this code:
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
ACAccount *twitterAccount = [arrayOfAccounts lastObject];
NSURL *requestURL = [NSURL URLWithString:#"https:api.twitter.com/1.1/account/verify_credentials.json"];
SLRequest *getRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:requestURL parameters:nil];
[getRequest setAccount:twitterAccount];
[getRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(#"Twitter Response : %# ",json);
}];
}
};
}];
}
With this code I'm getting this error:
Twitter Response : {
errors = (
{
code = 32;
message = "Could not authenticate you";
}
);
}
The weird thing is that, if I try to tweet something, or access some other endpoint of Twitter, where it's required to have an oauth-token, the operation ends successfully.
try this
TwitterResponse twitterResponse1 = TwitterStatus.Update(tokens, "test", new StatusUpdateOptions { APIBaseAddress = "https://api.twitter.com/1.1/", UseSSL = true });

Resources