[SalesForce] Customer Community login using REST API

I want to login a customer community user using rest api or at least using soap api.

I have created a user using the REST API. To create the user, first I created one contact with account and provided the profile id of a cloned profile of type community login user with api enabled option.

To Login user, I used the URL

https://my-domain-1-developer-edition.ap2.force.com/investor/services/oauth2/token

params

client_id,client_secret,username,password,
grant_type="password", response_type="code",content-type="application/x-www-form-urlencoded"

got response

{
  "error": "unsupported_grant_type",
  "error_description": "grant type not supported"
}

I have also tried to set 'application/x-www-form-urlencoded' as content-type. in header.

I have also tried to do it using SOAP
endpoint

https://my-domain-1-developer-edition.ap2.force.com/investor/services/Soap/u/36.0

<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:login xmlns:n1="urn:partner.soap.sforce.com">
      <n1:username>username</n1:username>
      <n1:password>password</n1:password>
    </n1:login>
  </env:Body>
</env:Envelope> 

got the response

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>sf:INVALID_LOGIN</faultcode>
            <faultstring>INVALID_LOGIN: Invalid username, password, security token; or user locked out.</faultstring>
            <detail>
                <sf:LoginFault xsi:type="sf:LoginFault">
                    <sf:exceptionCode>INVALID_LOGIN</sf:exceptionCode>
                    <sf:exceptionMessage>Invalid username, password, security token; or user locked out.</sf:exceptionMessage>
                </sf:LoginFault>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

I am using developer license.

Best Answer

Since community usernames are not globally unique, you need to use SOAP login with LoginScopeHeader to identify the org:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:partner.soap.sforce.com">
  <SOAP-ENV:Header>
    <ns1:LoginScopeHeader>
      <ns1:organizationId>00DB00000001234</ns1:organizationId>
    </ns1:LoginScopeHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:login>
      <ns1:username>user@example.com</ns1:username>
      <ns1:password>p455w0rd</ns1:password>
    </ns1:login>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Related Topic