iPhone Objective C oauth 2.0登录 - unsupported_grant_type

时间:2011-02-11 15:04:36

标签: iphone oauth salesforce nsurlrequest oauth-2.0

我是salesforce开发的新手,我正在尝试连接到Salesforce以获取令牌。

我想我的问题是2折: 1)我是否必须使用网页通过SF进行身份验证?

2)如果没有,为什么这不起作用?当我尝试使用用户名和密码进行身份验证时,我得到: {“error”:“unsupported_grant_type”,“error_description”:“不支持授权类型”}

这是我的代码:

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);

欢迎任何想法。

我确实尝试使用grant_type = authorization_code但又出现了另一个错误:

  

{“error”:“invalid_grant”,“error_description”:“授权无效   码“}

3 个答案:

答案 0 :(得分:3)

如果你想使用用户名/密码授权类型,那么它的类型是密码而不是基本凭证,要发送的正确参数集是

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

正如我在上一个问题中提到的,虽然这有效,但您确实希望使用基于Web的流程,以便支持使用SAML等替代登录服务的salesforce客户。

答案 1 :(得分:1)

1)是的,使用OAuth,您需要登录Salesforce并授予您的应用程序访问权限。对于iOS应用,您将查看客户端流Salesforce OAuth Client Flow Doc

2)在the Force.com developer site上查看适用于iOS的Toolkit。有一个名为ZKOAuthViewController的类,它几乎可以帮到你。其余的takelit基于Force.com的SOAP API,但我只是根据工具包创建了自己的OAuth登录控制器,而是使用REST API。

答案 2 :(得分:1)

Superfell的答案几乎是正确的。密码需要设置为:

密码+安全令牌

有关额外信息,请参阅http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_concepts_security.htm