You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS集成Square POS API:如何获取异账号Account_B的Location ID?

Solution to Location ID Mismatch with Square POS API

Let’s break down your problem first: your iOS app is configured with Account_A's client ID, but the Square Point of Sale app on the merchant's device is logged into their own Account_B. The error occurs because the location_id you’re passing in your SCCAPIRequest is tied to Account_A, which doesn’t match any locations under Account_B.

Here are two reliable ways to get the correct Location ID for Account_B:

1. Ask the Merchant to Retrieve It Manually (Quickest for Small-Scale Use)

Merchants can easily find their Location ID in their Square Dashboard (logged into Account_B):

  • Log into Square’s backend with Account_B credentials
  • Navigate to Settings > Business Information > Locations
  • Locate the relevant physical or virtual location, then copy the Location ID (it’s a string starting with L_)

You can either hardcode this ID for the merchant (not ideal for multiple merchants) or add a field in your app where the merchant can input their Location ID directly.

2. Automate Retrieval via Square OAuth & Locations API (Scalable for Multiple Merchants)

If you need to support multiple merchants with their own Square accounts, you’ll need to implement Square’s OAuth flow to get permission to access Account_B’s data. Here’s how:

Step 1: Initiate OAuth Authorization for Account_B

Use your Account_A client ID to redirect the merchant to Square’s OAuth authorization page. Make sure to include the locations_read scope to request access to their location data. When the merchant logs in with Account_B and grants permission, Square will send you an authorization code.

Step 2: Exchange Authorization Code for Account_B’s Access Token

Use the authorization code to request an access token tied to Account_B:

// Example HTTP request using URLSession
let tokenUrl = URL(string: "https://connect.squareup.com/oauth2/token")!
var tokenRequest = URLRequest(url: tokenUrl)
tokenRequest.httpMethod = "POST"
tokenRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

let tokenParams = [
    "client_id": "<account_a_client_id>",
    "client_secret": "<account_a_client_secret>",
    "code": "<authorization_code_from_merchant>",
    "grant_type": "authorization_code"
]
tokenRequest.httpBody = try? JSONSerialization.data(withJSONObject: tokenParams)

// Send the request to retrieve Account_B's access token

Step 3: Fetch Account_B’s Location IDs

With the access token for Account_B, call the Square Locations API to get all their location data:

let locationsUrl = URL(string: "https://connect.squareup.com/v2/locations")!
var locationsRequest = URLRequest(url: locationsUrl)
locationsRequest.httpMethod = "GET"
locationsRequest.setValue("Bearer <account_b_access_token>", forHTTPHeaderField: "Authorization")

// Parse the response to extract Location IDs
// The response includes an array of "locations" objects, each with an "id" field

You can then let the merchant select their preferred location from the list, or auto-select the primary one, and use that location_id in your SCCAPIRequest.

Critical Reminder

Never share your Account_A client secret with anyone, and ensure all OAuth flow steps are handled securely over HTTPS.


内容的提问来源于stack exchange,提问作者StoneLam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:55:52