如何通过App Store Connect API的价格点ID为新应用设置价格?
解决通过App Store Connect API设置应用价格失败的问题
问题背景
我正在编写Fastlane脚本上传新应用至App Store,但Fastlane无法为新创建的应用设置价格。于是我尝试用Ruby脚本通过App Store Connect API手动完成操作:成功获取到0美元对应的价格点ID,但调用PATCH接口设置价格时失败。以下是我的脚本:
lane :setAppPrice do auth_token = authAppstoreAPI() puts "Token: #{auth_token}" app_id = 'app_id_here' price_point_id = get_app_price_points(auth_token: auth_token, app_id: app_id) if price_point_id url = URI("https://api.appstoreconnect.apple.com/v1/#{app_id}/appPricePoints/#{price_point_id}") # Create the HTTP request for updating the app price http = Net::HTTP.new(url.host, url.port) http.use_ssl = true # Prepare the PATCH request headers = { "Authorization" => "Bearer #{auth_token}", "Content-Type" => "application/json" } request = Net::HTTP::Patch.new(url, headers) # Prepare the body with the new price data request.body = { data: { id: price_point_id, type: "appPricePoints", attributes: { customerPrice: 0.0 # Set your desired new price here } } }.to_json # Execute the request response = http.request(request) # Handle the response if response.code == "200" UI.success("Successfully updated app price to 0.0 for price point ID #{price_point_id}") else UI.error("Failed to update app price: #{response.body}") end end end lane :get_app_price_points do |options| require 'json' require 'net/http' auth_token = options[:auth_token] # Your App Store Connect API token app_id = options[:app_id] # The app ID for which you want to list subscriptions # Define the URL for fetching subscriptions url = URI("https://api.appstoreconnect.apple.com/v1/apps/#{app_id}/appPricePoints?filter%5Bterritory%5D=USA,CAN&include=territory") # Create the HTTP request http = Net::HTTP.new(url.host, url.port) http.use_ssl = true # Set up headers for the App Store Connect API request headers = { "Authorization" => "Bearer #{auth_token}", "Content-Type" => "application/json" } # Prepare the GET request request = Net::HTTP::Get.new(url, headers) # Execute the request response = http.request(request) # Handle the response if response.code == "200" app_prices = JSON.parse(response.body) # UI.message("Subscriptions for app #{app_id}: #{response.body}") # Use the find method to get the free price point ID directly free_price_point = app_prices["data"].find do |price_point| price_point["attributes"]["customerPrice"] == "0.0" end free_price_point_id = free_price_point ? free_price_point["id"] : nil UI.message("Free Price Point ID: #{free_price_point_id}") # Return the ID of the price point with 0.0 price free_price_point_id else UI.error("Failed to fetch subscriptions: #{response.body}") end end
核心问题与修复方案
1. 接口路径错误
你调用的/v1/{app_id}/appPricePoints/{price_point_id}是用于查询或修改单个价格点的接口,但应用的当前价格是独立的appPrice资源,正确的更新接口应为:PATCH /v1/apps/{appId}/appPrice
2. 请求体格式错误
设置应用价格时,不能直接修改customerPrice,而是需要通过relationships关联已有的价格点ID。App Store Connect要求价格必须对应系统预设的价格点,不能自定义数值。
3. 价格点查询逻辑优化
可以直接通过API参数过滤0美元价格点,无需本地遍历,提升效率。
修正后的完整脚本
lane :setAppPrice do auth_token = authAppstoreAPI() puts "Token: #{auth_token}" app_id = 'app_id_here' price_point_id = get_app_price_points(auth_token: auth_token, app_id: app_id) if price_point_id # 修正:操作appPrice资源而非appPricePoints url = URI("https://api.appstoreconnect.apple.com/v1/apps/#{app_id}/appPrice") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true headers = { "Authorization" => "Bearer #{auth_token}", "Content-Type" => "application/json" } request = Net::HTTP::Patch.new(url, headers) # 修正:通过relationships关联目标价格点 request.body = { data: { type: "appPrices", relationships: { pricePoint: { data: { id: price_point_id, type: "appPricePoints" } } } } }.to_json response = http.request(request) # 注意:成功响应可能是200或201 if response.code == "200" || response.code == "201" UI.success("成功将应用价格设置为0.0,价格点ID:#{price_point_id}") else UI.error("设置应用价格失败:#{response.body}") end end end lane :get_app_price_points do |options| require 'json' require 'net/http' auth_token = options[:auth_token] app_id = options[:app_id] # 优化:直接通过filter筛选0美元价格点 url = URI("https://api.appstoreconnect.apple.com/v1/apps/#{app_id}/appPricePoints?filter[territory]=USA,CAN&filter[customerPrice]=0.0&include=territory") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true headers = { "Authorization" => "Bearer #{auth_token}", "Content-Type" => "application/json" } request = Net::HTTP::Get.new(url, headers) response = http.request(request) if response.code == "200" app_prices = JSON.parse(response.body) free_price_point = app_prices["data"].first free_price_point_id = free_price_point ? free_price_point["id"] : nil UI.message("免费价格点ID:#{free_price_point_id}") free_price_point_id else UI.error("获取价格点失败:#{response.body}") end end
额外说明
- 确保你的API Token拥有
App Store Connect API > App Prices的读写权限; - 若需要设置多地区价格,需通过
appPriceTerritories资源进行操作,而非直接修改主价格; - 新创建的应用可能需要先完成基本信息配置,才能设置价格。
内容的提问来源于stack exchange,提问作者MetaSnarf
相关产品推荐
相关产品推荐

