i am using vimeo advance api for searching videos against query like in youtube and daily motion. vimeo advance api require oauth authentication . i want that vimeo return me json like the vimeo playground . https://developer.vimeo.com/apis/advanced/methods/vimeo.videos.search/playground
. i write the following code to get json data but no idea who i get json data
- (void)viewDidLoad
{
[super viewDidLoad];
SBJsonParser *json = [[SBJsonParser alloc]init];
// Do any additional setup after loading the view, typically from a nib.
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:#"0c5e6dd9b8fa5f8d91563332da03912ac8c2e15d"
secret:#"744eb15d911ee3607f7006f1f4ad7eb17a94eec6"];
NSURL *url = [NSURL URLWithString:#"https://vimeo.com/oauth/request_token"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:nil
realm:nil
signatureProvider:nil];
[request setParameters: [NSArray arrayWithObjects: [[OARequestParameter alloc] initWithName: #"oauth_callback" value: #"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.search&qdfduery=amir+khan"] ,nil]];
[request setHTTPMethod:#"GET"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenTicket:didFinishWithData:)
didFailSelector:nil];
}
- (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(#"data %#",requestToken);
OAMutableURLRequest *request;
if (self.accessToken != nil) {
self.accessToken = nil;
}
self.accessToken = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
NSLog(#"access token key %#",self.accessToken.key) ;
NSLog(#"access token secret %#",self.accessToken.secret) ;
NSURL *url = [NSURL URLWithString:#"https://vimeo.com/oauth/authorize"];
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:self.accessToken.key
secret:self.accessToken.secret];
request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:self.accessToken
realm:nil
signatureProvider:nil];
OARequestParameter *p0 = [[OARequestParameter alloc] initWithName:#"oauth_token" value:self.accessToken.key];
NSArray *params = [NSArray arrayWithObject:p0];
[request setParameters:params];
[webView loadRequest:request];
NSLog(#"request %#",request);
}
}
the above code return me access token . but i don't know how i can use that access token to get json like in vimeo playground . kindly anybody complete it for me i am so confused because on vimeo site their is no proper guideline how do i use advance api
Once you have an access token you need to make an authenticated request against the "http://vimeo.com/api/rest/v2" endpoint.
This request must follow the same oauth 1.0a signing process you performed while retrieving the oauth token.
You must also provide an api method via the querystring parameter, so to search for videos you would provide method=vimeo.videos.search. You can see an example of this on the playground under the "Request" header.
Any other parameters (such as format=json) are also handled via the querystring.
Related
i am getting this error in json format while fetching vimeo advanced search api this is the error
{"code":"401","expl":"The oauth_token passed has not been authorized by the user.","msg":"Invalid token"}}
this is my code for authentication on vimeo .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
consumer = [[OAConsumer alloc] initWithKey:#"xxxxxxxxxxxx"
secret:#"xxxxxxxxxxxx"];
NSURL *url = [NSURL URLWithString:#"https://vimeo.com/oauth/request_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 setParameters: [NSArray arrayWithObjects: [[OARequestParameter alloc] initWithName: #"oauth_callback" value: #"http://iosdevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html"] ,nil]];
[request setHTTPMethod:#"GET"];
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 {
NSLog(#"ticket value %#",ticket);
if (ticket.didSucceed) {
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
}
NSString *urlString = [NSString stringWithFormat:#"https://vimeo.com/oauth/authorize?auth_token=%#", requestToken.key];
NSURL *urlAuth = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:urlAuth];
NSURL *url = [NSURL URLWithString:#"http://vimeo.com/api/rest/v2?method=vimeo.videos.search"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:requestToken
realm:nil
signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]];
OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:#"format"
value:#"json"];
OARequestParameter *descParam = [[OARequestParameter alloc] initWithName:#"query"
value:#"amir khan"];
NSArray *params = [NSArray arrayWithObjects:nameParam, descParam, nil];
[request setParameters:params];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenTicket2:didFinishWithData:)
didFailSelector:#selector(requestTokenTicket2:didFinishWithData:)];
}
- (void)requestTokenTicket2:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data
{
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data string %#",dataString);
}
The error message "The oauth_token passed has not been authorized by the user" means that you are using the request token, and not the access token.
The request token is the token you get before the user has authenticated with your app.
This token is used in building the url that you send your users to.
Once the user has approved your app on vimeo.com, they will be redirected to your app. At this time you should exchange your request token for an access token.
I want to fetch all my friends from my LinkedIn profile. Please suggest me any tutorial.
My code is :
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,email-address,picture-url,Positions)"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:self.oAuthLoginView.consumer
token:self.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:)];
You can use this for connections:For request part see this.
- (void)connectionsApiCallResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"connectionsApiCallResult====%#",responseBody);
[responseBody release];
}
- (void)connectionsApiCallResult:(LOAServiceTicket *)ticket didFail:(NSError *)error
{
NSLog(#"%#",[error description]);
}
-(void)GetConnectionsCall
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/connections:(headline,first-name,last-name,picture-url,id)"];
LOAMutableURLRequest *request =
[[LOAMutableURLRequest alloc] initWithURL:url
consumer:self.consumer
token:TestAccessToken
callback:nil
signatureProvider:nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(requestTokenResult:didFinish:)
didFailSelector:#selector(requestTokenResult:didFail:)];
}
Please try the following steps
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"];
I'm trying to integrate LinkedIn in my iOS application, where I need to Authenticate, then fetch profile details and post to LinkedIn from my app. I do all these things quite successfully using this sample project LinkedIn OAuth Sample Client as reference, I did some modifications and everything is working quite well. And I use the details from
LinnkedIn API as well to implement this.
Next, I need to send invitation to connect in LinkedIn from the same app. I tried using Invitataion API .
Here is my code snippet:
-(void)sendInvitationToconnectTo:(NSString *)emailAddress withMessage:(NSString *)text
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSDictionary *temp=[[NSDictionary alloc]initWithObjectsAndKeys:#"/people/email=userName#gmail.com",#"_path",#"userName1",#"first-name",#"userName2",#"last-name", nil];
NSDictionary *temp2=[[NSDictionary alloc] initWithObjectsAndKeys:temp,#"person",nil];
NSArray *arr2=[[NSArray alloc]initWithObjects:temp2, nil];
NSDictionary *value=[[NSDictionary alloc]initWithObjectsAndKeys:arr2,#"values", nil];
NSDictionary *dict3=[[NSDictionary alloc]initWithObjectsAndKeys:#"friend",#"connect-type",nil];
NSDictionary *dict4=[[NSDictionary alloc] initWithObjectsAndKeys:dict3,#"invitation-request", nil];
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:dict4,#"item-content",#"Say yes!",#"body",#"Invitation to connect.",#"subject",value,#"recipients", nil];
NSString *updateString = [dict JSONString];
NSLog(#"updateString : %#",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:#"POST"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:#selector(sendRequestApiCallResult:didFinish:)
didFailSelector:#selector(sendRequestApiCallResult:didFail:)];
[request release];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSLog(#"invitation sent");
[self networkApiCall];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}
And my json string looks like this:
{"body":"Say yes!","subject":"Invitation to
connect.","recipients":{"values":[{"person":{"first-name":"userName1","last-name":"userName2","_path":"/people/email=userName#gmail.com"}}]},"item-content":{"invitation-request":{"connect-type":"friend"}}}
While sending the invitation I don't get any errors and when I debug, the sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data method gets called showing invitation sent text.
But ths issue is the target LinkedIn user didn't get any request!
Can anyone help me regarding this? Is there anything I'm doing wrong in my code?
NB I went through both the below mentioned links to get a solution, tried the solutions , but in vain.
1.Invitation API - Stumped by 401 (iOS and OAuthConsumer) - SOLVED
2.linkedin connect API gives error 401 - iphone
and the NSURLResponse statuscode I get is 401 !
EDIT:: Solved it myself. See my answer below.
Glad to inform all that after long 4 days of continuos research, I got this problem solved!
I got the solution from this Linkedin thread
You just need to make a few changes in the code:
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
[request setHTTPMethod:#"POST"];
NSString *messageToPerson = #"/people/email=target#gmail.com";
NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:messageToPerson,#"_path",#"TargetFirstName",#"first-name",#"TargetSecondName",#"last-name",nil] autorelease], #"person",nil];
NSArray *valueArray = [[NSArray alloc] initWithObjects:person,nil];
NSDictionary *values = [[NSDictionary alloc] initWithObjectsAndKeys:valueArray,#"values", nil];
NSDictionary *ir = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:#"friend",#"connect-type",nil] autorelease], #"invitation-request",nil];
NSDictionary *ic = [[NSDictionary alloc] initWithObjectsAndKeys:ir,#"item-content", nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:values,#"recipients",#"Invitation",#"subject",#"ConnectWithMe",#"body", ir, #"item-content", nil];
[request setValue:#"json" forHTTPHeaderField:#"x-li-format"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSLog(#"%#",[update description]);
NSString *updateString = [update JSONString];
[request prepare];
[request setHTTPBodyWithString:updateString];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request delegate:self
didFinishSelector:#selector(postUpdateApiCallResult:didFinish:)
didFailSelector:#selector(postUpdateApiCallResult:didFail:) withId:1];
[request release];
and
in oAdata OADataFetcher.m
- (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector withId:(int)idtm
{
[request release];
request = [aRequest retain];
delegate = aDelegate;
didFinishSelector = finishSelector;
didFailSelector = failSelector;
//note this change
if (idtm!=1) {
[request prepare];
}
connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
}
Try this. Will work for sure. Hope this will help future visitors.
Regards
I have written code to post content to linkedIn wall and it is working fine if I remove content dictionary from update dictionary. See the below code containing post data:
- (IBAction)postUpdate
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSDictionary *content=[[NSDictionary alloc] initWithObjectsAndKeys:#"http://www.celebs101.com/wallpapers/Bruce_Lee/421101/Bruce_Lee_Wallpaper.jpg",#"submitted-image-url",#"http://www.youtube.com/watch?v=GoZ2Be2zLq8",#"submitted-url",#"Post Image and Video testing",#"title",#"Posted Description",#"description",nil];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
#"anyone",#"code",nil], #"visibility",
#"Test posting to linkedIn", #"comment",content,#"content",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:)];
}
Post data comes in didfinishSelector is :
data after posting is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
<status>401</status>
<timestamp>1356601471318</timestamp>
<request-id>MFW58DCKE9</request-id>
<error-code>0</error-code>
<message>[unauthorized]. OAU:0onill9cburx|3c05c306-aad8-4d07-a2a1-2430aa21b54a|*01|*01:1356601465:Ji7pimMqrXp3RHCNJLv8iKZsklk=</message>
</error>
I don't know why so? please help..
Thanks in Advance..
I have solved it
working code to post on LinkedIn as follows:
- (IBAction)postUpdate
{
NSURL *url = [NSURL URLWithString:#"http://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:nil];
NSMutableDictionary *contentDic=[[NSMutableDictionary alloc] init];
[contentDic setValue:#"http://www.celebs101.com/wallpapers/Bruce_Lee/421101/Bruce_Lee_Wallpaper.jpg" forKey:#"submitted-image-url"];
[contentDic setValue:#"http://www.linkedin.com" forKey:#"submitted-url"];
[contentDic setValue:[NSString stringWithFormat:#"A title for your share"] forKey:#"title"];
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
#"anyone",#"code",nil], #"visibility",
#"Test posting to linkedIn",#"comment",contentDic,#"content",nil];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSString *updateString = [update JSONString];
NSLog(#"json string is %#",updateString);
[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
{
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data after posting is %#",myString);
}
- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(#"%#",[error description]);
}
If you are getting that type of error:
So it may be because of post url in "submitted-url" key.
So an easy way to do this first make the post_url for "submitted-url" short using this link( http://tinyurl.com/api-create.php?url=post_url). And then use this short post_url for "submitted-url" value. This can be a solution of your problem.
Sometimes long url for "submitted-url" key cause a that kind of problem.
I tried to post from twitter but it not posting the comments.
It display Posted successfully but not done.
can any one have an idea How to solve it or APIs is change for it.
Here Is code..
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:consumerKey secret:consumerSecret];
OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:url1 consumer:consumer token:nil realm:nil signatureProvider:nil] autorelease];
if (!request) return;
[request setHTTPMethod: #"POST"];
[request setParameters: [NSArray arrayWithObject: [[[OARequestParameter alloc] initWithName: #"oauth_callback" value:kLinkedInCallbackUrl] autorelease]]];
OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request delegate:self didFinishSelector: #selector(setRequestToken:withData:) didFailSelector: #selector(outhTicketFailed:data:)];
NSString *appendURL;
appendURL = [NSString stringWithFormat:#"?oauth_token=%#", _requestToken.key];
NSURL *url2 = [NSURL URLWithString:[authorizeURL stringByAppendingString:appendURL]];
//if (!_consumerKey && _consumerSecret) return nil; // we need a valid request token to generate the URL
request = [[[OAMutableURLRequest alloc] initWithURL:url2 consumer:consumer token:_requestToken realm:nil signatureProvider: nil] autorelease];
[request setParameters:[NSArray arrayWithObject: [[[OARequestParameter alloc] initWithName: #"oauth_token" value:_requestToken.key] autorelease]]];
//SET accessTOKEN key and secret
fetcher = nil;
fetcher = [[[OADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request delegate:self didFinishSelector: #selector(setAccessToken:withData:) didFailSelector: #selector(outhTicketFailed:data:)];
//Load WebView
// _webView.alpha = 100.0;
[_webView loadRequest:request];
Use mgtwitterengine for posting tweets from your application..