IPhone Objective C oauth 2.0 Login - unsupported_grant_type

I am new to Salesforce and trying to connect to Salesforce to get a token.

I think my question is 2 times: 1) Should I use a web page to authenticate with SF?

2) If not, why doesn't it work? When I try to use the username and password authentication method, I get: {"error": "unsupported_grant_type", "error_description": "grant type is not supported"}

Here is my code:

NSString *post = [NSString stringWithFormat:@"grant_type=basic-credentials&client_id=%@&client_secret=%@&redirect_uri=%@", kOAuthClientID, kOAuthClientSecret, kOAuthClientAuthURL ]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:kOAuthClientTokenUrl]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSError *error; NSURLResponse *response; NSLog(@"%@", request); NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; NSLog(@"%@", data); 

Any thoughts would be welcome.

I tried using grant_type = authorization_code, but got another error:

{"error": "invalid_grant", "error_description": "invalid authorization code"}

+2
source share
3 answers

If you want to use the username / password permission type, then its type is the password is not basic-credentials, the correct set of parameters for sending

 grant_type=password client_id=xxxxxxxxxx client_secret=1234567890 username=noreply@salesforce.com password=XXXXXXXXX 

As I mentioned in the previous question, although this works, you really want to use the flow on the Internet to support Salesforce clients who use alternative login services such as SAML.

+3
source

1) Yes, with OAuth you need to log in to Salesforce and provide access to your application. For an iOS app, you'll look at the Salesforce OAuth Client Flow Doc

2) Take a look at Toolkit for iOS on the Force.com developer site . There is a class called ZKOAuthViewController that pretty much does all this for you. The rest of takelit is based on Force.com's SOA API, but I just made my own toolkit-based OAuth login controller and used the REST API instead.

+1
source

Superfell's answer is almost correct. Password must be set to:

password + security token

See http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_concepts_security.htm for details.

+1
source

All Articles