Reputation: 848
I am developing an iOS app which require to get youtube channel and subscribe it. I can get list of channels without OAuth2 but for subscribe it I need to get access token by OAuth2 standard.
I can get access token by some handwork. But when I send this token into header with request into body for subscribe then it is giving me 'Bad Request (400)' Error.
NSString *URLString = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=%@", @"[mykey]"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",[self accessToken]] forHTTPHeaderField:@"Authorization"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer = requestSerializer;
NSDictionary *parameters = @{@"snippet":@{
@"resourceId":@{
@"channelId":[[[videoCountDetailArray objectAtIndex:0] valueForKey:@"snippet"] valueForKey:@"channelId"],
@"kind":@"youtube#channel"
}
}
};
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response ; %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failed");
}];
}
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fcfbccae3a0> { URL: https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=myKey } { status code: 400, headers {
"Cache-Control" = "private, max-age=0";
"Content-Encoding" = gzip;
"Content-Length" = 118;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Wed, 25 May 2016 09:28:57 GMT";
Expires = "Wed, 25 May 2016 09:28:57 GMT";
Server = GSE;
Vary = "Origin, X-Origin";
"alt-svc" = "quic=\":443\"; ma=2592000; v=\"34,33,32,31,30,29,28,27,26,25\"";
"alternate-protocol" = "443:quic";
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";
} },
Any help will be appriciated.
Upvotes: 2
Views: 526
Reputation: 848
I found the problem. There were two mistakes.
Actually, I was setting up different requestSerialiser and passing different serialiser.
I am passing APIKey into url for subscribe but No need to pass it.
My new code is
NSString *URLString = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/subscriptions?part=snippet"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",[self accessToken]] forHTTPHeaderField:@"Authorization"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer = requestSerializer;
NSDictionary *parameters = @{@"snippet":@{
@"resourceId":@{
@"channelId":[[[videoCountDetailArray objectAtIndex:0] valueForKey:@"snippet"] valueForKey:@"channelId"],
@"kind":@"youtube#channel"
}
}
};
NSLog(@"parameters : %@",parameters);
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response ; %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failed");
}];
Upvotes: 1