Getting page_access_token by graph api in FBConnect ios - iphone

Actually i want to upload photo on facebook fan page so for this i wrote below code
[m_facebook setAccessToken:#"BAAGgxy3PqqcBAHqByi2JOtTs .....8SCo8MK22y0smcFnxFEt7U6zVP2U4WpLrpnDWNuwXpvSYB9Btt7ZCMljBGmfxgPKoOdmadmNitSZB47trDv9hXd4wAE3VjZBbWBGMPP1lV8H1rfTcXNRuX8ePqRhxXsAypA7uHkSVyZASp0oaVfY0sJF55O8agZDZD"];
[m_facebook requestWithGraphPath:#"437...6356137/photos"
andParams:fbArguments
andHttpMethod:#"POST"
andDelegate:self];
With above code i am able to post photo on my facebook fan page but the problem is i have to hard code Page_Access_Token as you can see so can any one tell me that how i can access this Page_Access_Token token dynamically using FBConnect. I have already went through this link.

You have to authenticate by this line
NSArray *permissions = [[NSArray arrayWithObjects:#"read_stream", #"offline_access", #"publish_stream", #"manage_pages", #"user_photos", #"friends_photos",nil] retain];
[facebook authorize:FB_APP_ID permissions:permissions delegate:self];
Once you get authenticated , you will get AccessToken via this delegate method
- (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt
{
//Use access token
}
- (void)fbDidLogin {
[self.facebook1 accessToken];//you can access token once you get this call back.
}
Note: When you call extendAccessToken to extend token, the above delegate will call in that time too. fbDidLogin delegate method call when you get first time authentication. fbDidExtendToken delegate method get call when you try to extend access token. accessToken will get expired depending on expirationDate.

Related

Facebook Connect Posting a Status Update

HI I have got Facebook Connect working with a functional login and logout button. So far thats it, when I press a button a I want to post something to the user. Is there something I have to authorize? What is the precise steps for this. Thank you.
If you want to publish content to User's Feed you not required to authorize user if you use Feed Dialog for any other ways you required to authorize user prior to content publishing.
For web applications/sites (which isn't the case) there is also way to leverage automatic publishing of content user liked using Like Button social plugin by providing correct OpenGraph meta tags.
You really need to read documentation for iOS SDK, especially Dialogs and Authentication and Getting Started Guide.
if you want to set his status thought your FB app without showing a dialog you could do it like this
NSString *message = #"some text";
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, message,nil];
[fb requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self]; //fb here is the FaceBook instance.
and for sure you will do that after the user login and authorized the permissions .
To Authorize the permissions
if (![fb isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_likes", #"read_stream", nil];
[fb authorize:permissions];
[permissions release];
}

facebook dialog not sending request to friend

I am trying to use the facebook dialog to send request to my friend using the following:
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Come check out my app.", #"message",
nil];
[facebook dialog:#"apprequests"
andParams:params
andDelegate:self];
the delegate - (void)dialogDidComplete:(FBDialog *)dialog { was triggered but apparently I dont see anything on my friends account. I've added him as a tester of the app through the developer account. Why is this?
From https://developers.facebook.com/docs/requests/:
User to User Requests are only available for Canvas apps, not
websites, as accepting a request will direct the user to the Canvas
Page URL of the app that sent the Request.
Do you have a canvas url specified in your app settings?
Also what does the response look like from Facebook at the completion of the dialog? It should look like:
{
request: 'REQUEST_OBJECT_ID'
to:[array of USER_IDs]
}

How to retrieve username for facebook from sharekit in iphone sdk

I am creating an app for Facebook and twitter integration using sharekit. That works fine. But now I want to retrieve the username and password for facebook login. I followed this link.
But I am not able to retrieve these..
I can't understand how can use that method and where can i use to retrieve username and password
Can you please guide me if you know.
First of all, you can't retrieve a user's Facebook password. Hopefully the reasons for this are obvious.
You can, however, retrieve the access token that your app is granted once you connect your app to a user's Facebook account using ShareKit.
As of this writing, I don't believe ShareKit makes the access token accessible directly, but there's an easy hack to retrieve it.
Step 1: Ensure that you're app is authorized to connect to Facebook
BOOL isConnected = [SHKFacebook isServiceAuthorized];
If you get isConnected == NO here, your UI should indicate that the user needs to connect to Facebook to use your sharing features.
Step 2: Get the access token in order to access the user's Facebook data
Assuming you got isConnected == YES in step 1
// Hack into ShareKit's user defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [defaults valueForKey:#"kSHKFacebookAccessToken"];
Step 3: Bypass ShareKit and make a custom query to the Facebook SDK
Assuming a property in your class such as this one...
// Change "strong" to "retain" if not using ARC
#property (nonatomic, strong) SHKFacebook *shkFb;
...you can start a Facebook query with something like this...
if ( !fb ) {
// This is how SHKFacebook instantiates a Facebook object. YMMV.
self.fb = [[Facebook alloc] initWithAppId:SHKCONFIG(facebookAppId)];
}
NSMutableDictionary *fbParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"name", #"fields",
accessToken, #"access_token",
nil];
[fb requestWithGraphPath:#"me" andParams:fbParams andDelegate:self];
Step 4: Implement the Facebook delegate methods
Once the Facebook query's done, it'll notify your object, at which point you can do fancy things such as display the user's name to make it clear whose wall will get posts sent from your app.
You'll need to declare the FBRequestDelegate protocol in your .h, of course:
#import "Facebook.h"
#interface YourClass : NSObject <FBRequestDelegate>
And you'll need to implement (minimally) the success and failure methods from FBRequestDelegate:
#pragma mark - FBRequestDelegate
- (void)request:(FBRequest *)request didLoad:(id)result {
// Additional keys available in "result" can be found here:
// https://developers.facebook.com/docs/reference/api/user/
NSString *username = [result objectForKey:#"name"];
// Localize if you're at all interested in the global app market!
NSString *localizedString = NSLocalizedString(#"connected as %#",
#"Connection status label");
// The label will read "connected as <username>"
self.statusLabel.text = [NSString stringWithFormat:localizedString, username];
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
// Handle failure
// (In our app, we call [SHKFacebook logout]
// and display an error message to the user with
// an option to retry connecting to Facebook.)
}
I believe clozach's answer is for an older version of ShareKit. In the most recent (as of Jan 30 2012) the method of acquiring the Facebook Access Token provided fails with accessToken always being nil.
// Hack into ShareKit's user defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [defaults valueForKey:#"kSHKFacebookAccessToken"];
Instead, using
NSString *accessToken = [[FBSession activeSession] accessToken];
works for me.
ShareKit uses FBConnect internally for authentication. So the values can't be retrieved using the same method as you would for Twitter. FBConnect uses a UIWebView view to connect to the server while authenticating. After authentication, the app will store a token which can be reused to publish the text until the user discards the token on Facebook.
So, the answer is no. You can't get that data.

FaceBook iOS - check if my facebook app is allready authorized

My question is how to check if my FaceBook app is already authorized for posts by the user, can't find any info on that.
I'm using:
Facebook* facebook = [[Facebook alloc] initWithAppId:#"1234567"];
[facebook authorize:[NSArray arrayWithObjects:#"read_stream", #"offline_access",nil] delegate:self];
A dialog pops up asking me to authorize the app, when done i'm all fine, can do a:
[facebook dialog:#"feed" andDelegate:self];
to post notes on that app.
But, if the user blocks or removes the app i want to do the authorize again before showing the dialog for posting, can't find a way of getting that kind of info before calling authorize.
Any help is appreciated.
Thanks.
I had to deal with this issue too.
When calling the dialog method, you send a delegate that should conform to FBDialogDelegate, which has a method that is called when the dialog fails to load due an error. But in the case the app has been unauthorized, the dialog shows a login screen to the user, but after setting the user and password, a second form appears, letting the user know that an error has occurred. The delegate is also called, but the error received just states that he method has failed with no exact reason why, or even an error number. This method should be called with the correct error, before anything, so the application could act accordingly.
So I found a work around, maybe this is not the best way, but it certainly works. Any call that you do to the Facebook graph api via a request, will fail if the app has been unauthorized by the user. So what I did was to check that before calling the feed dialog method.
Add the following line where you need to test if the app is still authorized:
if ([facebook isSessionValid])
//isSessionValid only checks if the access token is set, and the expiration date is still valid. Lets make a call and see if we really are authorized to post to this user from this app.
[facebook requestWithGraphPath:#"me" andDelegate:self];
else
//authorize Facebook connect
This will just call the method that returns the basic information from the user. If everything is fine, the following method will be called from the delegate:
- (void)request:(FBRequest *)request didLoad:(id)result
{
//Everything is ok. You can call the dialog method. It should work.
}
If the app has been unauthorized by the user, the following method from the delegate will be called:
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error;
{
NSString *type = [[[error userInfo] objectForKey:#"error"] objectForKey:#"type"];
if (type)
{
if ([type isEqualToString:#"OAuthException"]) //aha!
{
//user has unauthorized the app, lets logout from Facebook connect. Also clear the access and expiration date tokens
[facebook logout:self];
//Call the authorize method again. Or let the user know they need to authorize the app again.
}
}
}
So, as I said before, not the best way, but gets the job done. Hopefully, Facebook will add a method to check for this specific scenario, or add a new method to the delegate that deals with the unauthorized app issue.
I'm not sure how exactly to do it with Facebook's SDK, but you can use FQL to query the permissions. The query URL would look something like
https://api.facebook.com/method/fql.query?query=SELECT+uid,+read_stream,+offline_access+FROM+permissions+WHERE+uid=me()&access_token=...
It looks like requestWithMethodName:andParams:andHttpMethod:andDelegate: passing fql.query as the method is the way to go, as long as you can arrange for isSessionValid to be true (or somehow supply access_token in the params yourself).

Getting friends of friends with Facebook iOS SDK

Is it possible to get information about friends of my friends using facebook ios sdk?
Please.If it is possible give an example.
request with Graph api #'[friend_id]/friends" does not work.
'code' [facebook requestWithGraphPath:#"[friend_id]/friends" andDelegate:self];'code'
i try this code but it returns
Error:facebookErrDomain error 10000 where'code'
facebook = [[ Facebook alloc] initWithAppId:app_id];
NSArray *permissions = [NSArray arrayWithObjects:#"I try all permissions"];
[facebook authorize:permissions delegate:self]; 'code'
user is logged in, All information about this friend is available
I'm quite sure this is not possible, you'ld need data access privs of the friends to get their friends data. this has to do with the access rights on fb and this is good for some reasons it is like it is. even if this data protection mechanism disturbs your business idea ;)
If you want catch friend informations you must :
Alloc and Init a Facebook object
Facebook *facebookObject = [[Facebook alloc] init];
Check permissions
NSArray *permissions = [NSArray arrayWithObjects:#"publish_stream", #"offline_access",nil];
Authorize facebook
[facebookObject authorize:APP_ID permissions:permissions delegate:self];
After that you can used
[facebookObject requestWithGraphPath:#"me/friend" andDelegate:self];
In delegate methods
- (void)request:(FBRequest *)request didLoad:(id)result
You catch the result who must be a NSDictionnary.