In my app user has a option to login to app through linkedin.after login i have to get user's connections. when i try to get users connections i am getting response as
{
"errorCode": 0,
"message": "Access to connections denied",
"requestId": "60A0DS1MZE",
"status": 403,
"timestamp": 1386682428799
}
Here is my code
-(void)GetConnectionsCall
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:_oAuthLoginView.consumer
token:_oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(connectionsApiCallResult:didFinish:)
didFailSelector:#selector(connectionsApiCallResult:didFail:)];
}
- (void)connectionsApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"connectionsApiCallResult====%#",responseBody);
}
- (void)connectionsApiCallResult:(OAServiceTicket *)ticket didFail:(NSError *)error
{
NSLog(#"%#",[error description]);
}
if you are using OAuth Starter Kit try this . . . .
first go to https://www.linkedin.com/secure/developer edit your App scope , make sure you have checked the r_network scope .
then go to OAuthLoginView of you project , then go to -(void) requestTokenFromProvider method .
edit you scope in below lines .
OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:#"scope"
value:#"r_fullprofile+rw_nus+r_network"];
OARequestParameter * scopeParameter=[OARequestParameter requestParameter:#"scope"
value:#"r_fullprofile rw_nus r_network"];
You need to ask for permissions.. When this is not done you are not allowed to query this information.
Everything documented here: http://developer.linkedin.com/documents/connections-api
Related
I have an iOS Application which authenticates with the Google Plus API via OAuth 2.0. The issue I have is that I can't POST my moment even though I have followed the Google Documentation exactly. Here is my POST request:
NSString *gp_moment = [NSString stringWithFormat:#"https://www.googleapis.com/plus/v1/people/me/moments/vault?access_token=%#", token_gplus];
NSString *urlString = gp_moment;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
// Set the header - Content-Type.
NSDictionary *the_header = #{#"Content-type" : #"application/json",
#"userId" : #"me",
#"collection" : #"vault",
#"requestvisibleactions" : #"http://schemas.google.com/AddActivity"};
[request setAllHTTPHeaderFields:the_header];
// Set the metadata for the GP Moment (eg: name). - request_visible_actions
NSDictionary *metadata = #{#"target" : #{
#"id" : #"replacewithuniqueidforaddtarget",
#"image" : #"http://www.google.com/s2/static/images/GoogleyEyes.png",
#"type": #"http://schema.org/CreativeWork",
#"description" : #"test_desc",
#"name" : #"TESTNAME",
},
#"type" : #"http://schemas.google.com/AddActivity"};
// Convert metadata into JSON format and submit.
NSError *jError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:metadata options:NSJSONWritingPrettyPrinted error:&jError];
[request setHTTPBody:jsonData];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *returnedData;
returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *headers = [response allHeaderFields];
NSLog(#"DATA: %#", [[NSString alloc] initWithData:returnedData encoding:NSASCIIStringEncoding]);
NSLog(#"%#", headers);
NSLog(#"%#", response);
NSLog(#"%#", error);
UPDATE - HOW I AM GETTING THE OAUTH TOKEN
I am getting the token by using the GTMOAuth2 iOS library provided by Google. It has 4 main classes which I am using:
This is the code I have implemented from the classes into my ViewController:
-(GTMOAuth2Authentication *)begin_authorization {
// Set the token URL to the token endpoint.
NSURL *tokenURL;
// Set a bogus redirect URI. It won't actually be used as the redirect will
// be intercepted by the OAuth library and handled in the app.
NSString *redirectURI;
GTMOAuth2Authentication *auth;
tokenURL = [NSURL URLWithString:#"https://accounts.google.com/o/oauth2/token"];
redirectURI = #"http://localhost:3000/oauth/callback/";
auth = [GTMOAuth2Authentication authenticationWithServiceProvider:#"GooglePlus" tokenURL:tokenURL redirectURI:redirectURI clientID:kMyClientID_gplus clientSecret:kMyClientSecret_gplus];
[auth setScope:#"https://www.googleapis.com/auth/plus.login"];
// Set the appropriate token type.
[auth setTokenType:#"Bearer"];
return auth;
}
-(void)authorize:(NSString *)service {
GTMOAuth2Authentication *auth = [self begin_authorization];
// Prepare the Authorization URL. We will pass in the name of the service
// that we wish to authorize with.
NSURL *authURL;
authURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://accounts.google.com/o/oauth2/auth/"]];
NSString *keyname;
keyname = [NSString stringWithFormat:#"GooglePlus"];
// Display the authentication view
GTMOAuth2ViewControllerTouch *viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth authorizationURL:authURL keychainItemName:keyname delegate:self finishedSelector:#selector(viewController:finishedWithAuth:error:)];
[viewController setBrowserCookiesURL:[NSURL URLWithString:#"https://accounts.google.com/o/oauth2/auth/"]];
// Push the authentication view to our navigation controller instance
[[self navigationController] pushViewController:viewController animated:YES];
}
-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error {
if (error != nil) {
// Authentication failed
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Authorization Failed" message:[error localizedDescription] delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alertView show];
}
else {
// Authentication succeeded
// Assign the access token to the instance property for later use
account_check = auth.accessToken;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:account_check forKey:#"gp_token"];
[defaults synchronize];
}
}
In the GTMOAuth2SignIn.m file I am also setting the offline_access and "requestvisisbleactions" parameters like so:
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"code", #"response_type",
clientID, #"client_id",
scope, #"scope", // scope may be nil
#"force", #"approval_prompt",
#"offline", #"access_type",
#"http://schemas.google.com/AddActivity", #"requestvisibleactions",
nil];
UPDATE - 2
I have tried looking through all the Google Plus documentation as well as looking through the differences between "requestvisisbleactions" and "request_visisble_actions" and still nothing works. Does anyone know what is wrong?
UPDATE - 3 - The error I am still getting
I keep on getting this error from Google Plus API. I just don't know why...:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "unauthorized",
"message": "Unauthorized"
}
],
"code": 401,
"message": "Unauthorized"
}
}
I believe that you need to use request_visible_actions. So your parameters might be set as
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"code", #"response_type",
clientID, #"client_id",
scope, #"scope", // scope may be nil
#"force", #"approval_prompt",
#"offline", #"access_type",
#"http://schemas.google.com/AddActivity", #"request_visible_actions",
nil];
I am trying to fetch Tumblr home page feed in iOS using Tumblr V2 API's.
Here is my code for the fetch feed:
-(void)requestFeed
{
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:kTumblrConsumerKey
secret:kTumblrConsumerSecret];
TumblrUser *tumblrUser = [Utils currentUser];
NSString *username = (tumblrUser!=nil)?tumblrUser.username:#"";
NSString *requestUrl = [NSString stringWithFormat:#"api.tumblr.com/v2/blog/%#/posts/queue", username];
OAToken *authToken = [[OAToken alloc] initWithUserDefaultsUsingServiceProviderName:kTumblrAccessTokenDefaultsKey prefix:#"TumblrConnect"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestUrl]
consumer:consumer
token:authToken
realm:nil // our service provider doesn't specify a realm
signatureProvider:nil]; // use the default method, HMAC-SHA1
[consumer release];
[authToken release];
[request setHTTPMethod:#"GET"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(FeedTicket:didFinishWithData:)
didFailSelector:#selector(FeedTicket:didFailWithError:)];
}
But i am getting below error in response.
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo=0x8a995a0
{NSErrorFailingURLStringKey=api.tumblr.com/v2/blog/coocku/posts/queue,
NSErrorFailingURLKey=api.tumblr.com/v2/blog/coocku/posts/queue,
NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x8c997c0
"unsupported URL"}
Add url scheme to your url
e.g.
[NSURL URLWithString:#"http://www.api.tumblr.com/v2/blog/%#/posts/queue];
OR
[NSURL URLWithString:#"https://www.api.tumblr.com/v2/blog/%#/posts/queue];
Hi I want share text in LinkedIn through my app. My code is below here...
This method when I click the btn linkedIn share..
- (void)linkedBtnEvent
{
if(oAuthLoginView != nil) {
oAuthLoginView.delegate = nil;
oAuthLoginView = nil;
}
oAuthLoginView = [[OAuthLoginView alloc] initWithNibName:nil bundle:nil];
oAuthLoginView.delegate=self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewDidFinish:)
name:#"loginViewDidFinish"
object:self.oAuthLoginView];
[self presentViewController:self.oAuthLoginView animated:YES completion:nil];
}
This are the method to handle share after login..
-(void) loginViewDidFinish:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self profileApiCall];
}
- (void)profileApiCall
{
NSURL *url = [NSURL URLWithString:#"https://api.linkedin.com/v1/people/~"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(profileApiCallResult:didFinish:)
didFailSelector:#selector(profileApiCallResult:didFail:)];
}
- (void)profileApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *profile = [responseBody objectFromJSONString];
if ( profile )
{
NSLog(#"%#", [[NSString alloc] initWithFormat:#"%# %#",
[profile objectForKey:#"firstName"], [profile objectForKey:#"lastName"]]);
}
// The next thing we want to do is call the network updates
[self networkApiCall];
}
- (void)profileApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}
- (void)networkApiCall
{
NSURL *url = [NSURL URLWithString:#"https://api.linkedin.com/v1/people/~/network/updates?scope=self&count=1&type=STAT"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(networkApiCallResult:didFinish:)
didFailSelector:#selector(networkApiCallResult:didFail:)];
}
- (void)networkApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
if (isSharedLinked)
{
NSLog(#"Shared Successfully");
linkedBtn.enabled = NO;
}
else
{
isSharedLinked = YES;
[self performSelector:#selector(postTextLinkedIn) withObject:nil afterDelay:1];
}
}
- (void)networkApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}
- (void)postTextLinkedIn
{
NSURL *url = [NSURL URLWithString:#"https://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
#"anyone",#"code",nil], #"visibility",
#"Wow its working... Share the text in Linked In", #"comment", nil];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSString *updateString = [update JSONString];
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:#"POST"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(postUpdateApiCallResult:didFinish:)
didFailSelector:#selector(postUpdateApiCallResult:didFail:)];
}
- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
// The next thing we want to do is call the network updates
[self networkApiCall];
}
- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}
It shows no error it always haow it share successfully in LinkedIn but no text share in that..
Please help me to fix this issue... I cant figure what mistake I done..
You need to add "rw_nus" in "scope". This only permit the app to share updates... This is your mistake otherwise all your code correct man..
- (void)requestTokenFromProvider
{
OAMutableURLRequest *request =
[[[OAMutableURLRequest alloc] initWithURL:requestTokenURL
consumer:self.consumer
token:nil
callback:linkedInCallbackURL
signatureProvider:nil] autorelease];
[request setHTTPMethod:#"POST"];
OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:#"scope"
value:#"r_fullprofile+r_contactinfo+r_emailaddress+r_network+r_basicprofile+rw_nus"];
NSArray *params = [NSArray arrayWithObjects:nameParam, nil];
[request setParameters:params];
OARequestParameter * scopeParameter=[OARequestParameter requestParameter:#"scope" value:#"r_fullprofile r_contactinfo r_emailaddress r_network r_fullprofile rw_nus"];
[request setParameters:[NSArray arrayWithObject:scopeParameter]];
OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenResult:didFinish:)
didFailSelector:#selector(requestTokenResult:didFail:)];
}
Replace this is in your oAuthloginView.m
You can use a great lib for LinkedIn sharing: https://www.cocoacontrols.com/controls/linkedin-share
or directly from GitHub: https://github.com/pmilanez/MIS-Linkedin-Share
It is covered with MIT License and can be used in commercial project as well.
Hope this helps.
My code looks something like this :
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:#"my_ClientID"
secret:#"my_Secret"];
NSURL *url = [NSURL URLWithString:#"https://launchpad.37signals.com/authorization/token"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:nil // we don't have a Token yet
realm:nil // our service provider doesn't specify a realm
signatureProvider:nil]; // use the default method, HMAC-SHA1
[request setHTTPMethod:#"POST"];
NSLog(#"USER URL : %#",[request URL]);
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenTicket:didFinishWithData:)
didFailSelector:#selector(requestTokenTicket:didFailWithError:)];
.
- (void)requestTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
if (ticket.didSucceed) {
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
OAToken *requestToken = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
NSLog(#"%#",requestToken);
}
}
My Delegate method never gets a (ticket.didSucceed) Success.
Can someone tell me what I am missing here?
Thanks
It looks like you are trying to have the user authorize your app and generate the token for the first time. For this, you're using the wrong URL. From the 37Signals API:
Your app requests authorization by redirecting your user to Launchpad:
https://launchpad.37signals.com/authorization/new?type=web_server&client_id=your-client-id&redirect_uri=your-redirect-uri
The URL you are using is for getting the access token from the verification code.
I am trying to make a basic authentication system in iOS that sends a POST to Django and on the Django side authenticates the user and starts a session. Right now I am able to send the user information by passing the values as data in the URL and authenticating it, but how do I retrieve the session data or cookie from the Django response? When I try to store or print out the cookie, it tells me the array is empty. I have tried both request.requestCookies and request.responseCookies.
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"test_user", #"username", #"pass", #"password", nil];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://127.0.0.1:8000/login/"]];
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
if ( error ) {
NSLog( #"ERROR - %#", error.localizedDescription );
} else {
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader: #"Content-Type" value:#"application/json; charset=utf-8"];
[request appendPostData:data];
[request setRequestMethod:#"POST"];
[request setCompletionBlock:^{
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:#"Login"
message:#"Login was sent"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[alerView performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
NSLog(#"RESPONSE: %#", [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding]);//, [request.requestCookies objectAtIndex:0]);
NSLog(#"COOKIE: %#", [request.requestCookies objectAtIndex:0]);
[ASIHTTPRequest addSessionCookie:[request.requestCookies objectAtIndex:0]];
}];
Okay, so I resolved this issue and it turns out that on the server side I was putting in the data but not officially logging in with Django (was not returning the proper cookies) which meant my app was not receiving the proper header.