asa(苹果Apple Search Ads平台)授权调用接口

2023-10-22 00:10

本文主要是介绍asa(苹果Apple Search Ads平台)授权调用接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、OAuth 认证

1. 认证流程

Implementing OAuth for the Apple Search Ads API

This implementation process guides you through the following steps:

  • Invite users with API permissions.
  • Generate a private-public key pair.
  • Extract a public key from your persisted private key.
  • Upload a public key.
  • Create a client secret.
  • Request an access token.
  1. 邀请拥有api权限的用户
  2. 生成专用公钥对
  3. 从持久化私钥中提取公钥
  4. 上传公钥
  5. 创建客户端密钥
  6. 请求访问令牌

2. 邀请用户

Invite Users

Account administrators invite users with API permissions using the following process:

  • From the Apple Search Ads UI, choose Sign In > Advanced and log in as an account administrator.
  • From the Users menu in the top-right corner, select the account to invite users to.
  • Choose Account Settings > User Management.
  • Click Invite Users to invite users to your Apple Search Ads organization.
  • In the User Details section, enter the user’s first name, last name, and Apple ID.
  • In the User Access and Role section, select an API user role. For non-API roles, see Invite users to your account.
  • Click Send Invite. The invited user receives an email with a secure code. The user signs into the secure Apple URL in the email and inputs the provided code, which activates the user’s account.

管理员账号通过以下过程邀请用户具有api权限:

  • 从Apple Search Ads UI中,选择“登录”>“高级(Advanced)”,然后以帐户管理员身份登录

登陆网址

图2-1:登陆界面

  • 从右上角的“用户”菜单中,选择要邀请用户加入的帐户。
  • 选择“帐户设置”>“用户管理”。
  • 单击邀请用户以邀请用户加入您的Apple Search Ads组织。
  • 在“用户详细信息”部分,输入用户的名字、姓氏和Apple ID。
  • 在“用户访问权限和角色”部分中,选择一个API用户角色(Read Only、API Read Only)。有关非API角色,请参阅邀请用户加入您的帐户。单击发送邀请。受邀用户收到一封带有激活码的电子邮件。用户登录电子邮件中的安全Apple URL并输入提供的激活码,从而激活用户的帐户:

整个过程开发者需要提供 Apple ID,具有苹果广告账户管理员权限的账号授权对应子账户的【最低 API 只读】权限;用户前往授权 Apple ID 邮箱查收邀请邮件发送对应的【激活码】,点击接受邀请,此时跳转至苹果广告登录后台,输入邀请【激活码】,完成苹果广告后台登录流程;

图2-2:Apple ID 邮箱查收邀请邮件并跳转激活账号

3. 密钥管理

需要下载openssl

  • openssl生成私钥文件:openssl ecparam -genkey -name prime256v1 -noout -out private-key.pem

Parameter

Description

-name

prime256v1 — the Elliptic Curve Digital Signature Algorithm (ECDSA) filename.

prime256v1:椭圆曲线数字签名算法(ECDSA),key算法对应EC

-out

The .pem filename where you generate and store the key pair.

文件名

  • openssl通过私钥文件抽取公钥文件:openssl ec -in private-key.pem -pubout -out public-key.pem

Parameter

Description

-in

The private key filename: private-key.pem

输入文件名

-out

The ec256-public-key file where you generate and store the public key.

输出文件名

  • openssl通过私钥文件生成 pkcs8 编码文件: openssl pkcs8 -topk8 -nocrypt -in private-key.pem -out pkcs8_rsa_private_key.pem
  • 将公钥粘贴到 ASA 后台(设置 > API),生成 clientId, teamId, keyId

图3-1:通过公钥生成clientId,teamId,keyId

4. 代码生成服务端token

将asa后台生成的 clientId, teamId, keyId以及私钥生成的 pkcs8 编码字符串拿出来备用

1. java代码

依赖

<dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.10.3</version>
</dependency>

代码

public static void main(String[] args) throws Exception {// 需要保存的密钥信息String pkcs8PrivateKey = "MIxxxxxx";String keyId = "8xxx8";String teamId = "Sxxxb";String clientId = "Sxxxb";String audience = "https://appleid.apple.com";String alg = "ES256";// 获取私钥keybyte[] decodeBase64Key = Base64.decodeBase64(pkcs8PrivateKey);// ec Keys for the Elliptic Curve algorithm. 椭圆螺线加密算法KeyFactory keyFactory = KeyFactory.getInstance("EC");PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodeBase64Key);PrivateKey privateKey = keyFactory.generatePrivate(keySpec);// 构建载荷Map<String, Object> headers = new HashMap<>();headers.put("alg", alg);headers.put("kid", keyId);long iat = DateUtil.currentSeconds();long exp = iat + 86400 * 180;Map<String, Object> payload = new HashMap<>();payload.put("sub", clientId);payload.put("aud", audience);payload.put("iat", iat);payload.put("exp", exp);payload.put("iss", teamId);// 生成tokenString jwtToken = Jwts.builder().setClaims(payload).setHeaderParams(headers).signWith(SignatureAlgorithm.ES256, privateKey).compact();
}

2. python代码

import os
import datetime as dt
from authlib.jose import jwt
from Crypto.PublicKey import ECCprivate_key_file = "private-key.pem"
public_key_file = "public-key.pem"
client_id = "SEARCHADS.9703f56c-10ce-4876-8f59-e78e5e23a152"
team_id = "SEARCHADS.9703f56c-10ce-4876-8f59-e78e5e23a152"
key_id = "d136aa66-0c3b-4bd4-9892-c20e8db024ab"
audience = "https://appleid.apple.com"
alg = "ES256"# Create the private key if it doesn't already exist.
if os.path.isfile(private_key_file):with open(private_key_file, "rt") as file:private_key = ECC.import_key(file.read())
else:private_key = ECC.generate(curve='P-256')with open(private_key_file, 'wt') as file:file.write(private_key.export_key(format='PEM'))# Extract and save the public key.
public_key = private_key.public_key()
if not os.path.isfile(public_key_file):with open(public_key_file, 'wt') as file:file.write(public_key.export_key(format='PEM'))# Define the issue timestamp.
issued_at_timestamp = int(dt.datetime.utcnow().timestamp())
# Define the expiration timestamp, which may not exceed 180 days from the issue timestamp.
expiration_timestamp = issued_at_timestamp + 86400*180# Define the JWT headers.
headers = dict()
headers['alg'] = alg
headers['kid'] = key_id# Define the JWT payload.
payload = dict()
payload['sub'] = client_id
payload['aud'] = audience
payload['iat'] = issued_at_timestamp
payload['exp'] = expiration_timestamp
payload['iss'] = team_id# Open the private key.
with open(private_key_file, 'rt') as file:private_key = ECC.import_key(file.read())# Encode the JWT and sign it with the private key.
client_secret = jwt.encode(header=headers,payload=payload,key=private_key.export_key(format='PEM')
).decode('UTF-8')# Save the client secret to a file.
with open('client_secret.txt', 'w') as output:output.write(client_secret)

载荷示例

// Header
{
"alg": "ES256",
"kid": "d136aa66-0c3b-4bd4-9892-c20e8db024ab"
}
// Payload
{
"iss": "SEARCHADS.9703f56c-10ce-4876-8f59-e78e5e23a152",
"iat": 2234567891,
"exp": 2234567900,
"aud": "https://appleid.apple.com",
"sub": "SEARCHADS.9703f56c-10ce-4876-8f59-e78e5e23a152"
}

检查client_secret是否正确:JSON Web Tokens - jwt.io

5. 请求获取accesstoken

curl -X POST \
-H 'Host: appleid.apple.com' \
-H 'Content-Type: application/x-www-form-urlencoded' \
'https://appleid.apple.com/auth/oauth2/token?grant_type=client_credentials&
client_id=SEARCHADS.27478e71-3bb0-4588-998c-182e2b405577&client_secret=eyJ0
eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.zI1NiIsImprdSI6Imh0dHBzOi8vYXV0aC5kZXYuYXB
pLnJpY29oL3YxL2Rpc2NvdmVyeS9rZXlzIiwia2lkIjoiMmIyZTgyMTA2NzkxZGM4ZmFkNzgxNW
Q3ZmI1NDRhNjJmNzJjMTZmYSJ9.eyJpc3MiOiJodHRwczovL2F1dGguZGV2LmFwaS5yaWNvaC8iL
CJhdWQiOiJodHRwczovL2lwcy5kZXYuYXBpLnJpY29oLyIsImlhdCI6MTQ5MDg1Mjc0MSwiZXhwI
joxNDkwODU2MzQxLCJjbGllbnRfaWQiOiI4ODQwMWU1MS05MzliLTQ3NzktYjdmNy03YzlmNGIzZj
kyYzAiLCJzY29wZSI6Imh0dHBzOi8vaXBzLmRldi5hcGkucmljb2gvdjEiLCJyaWNvaF9tc3Mi
OnsibWVkaWEiOnsicXVvdGEiOjEwLCJ0aHJvdHRsZSI6eyJ2YWx1ZSI6MCwid2luZG93IjowfX1
9fQ.jVq_c_cTzgsLipkJKBjAHzm8KDehW4rFA1Yg0EQRmqWmBDlEKtpRpDHZeF6ZSQfNH2OlrBW
FBiVDV9Th091QFEYrZETZ1IE1koAO14oj4kf8TCmhiG_CtJagvctvloW1wAdgMB1_Eubz9a8oim
cODqL7_uTmA5jKFx3ez9uoqQrEKZ51g665jSI6NlyeLtj4LrxpI9jZ4zTx1yqqjQx0doYQjBPhOB
06Z5bdiVyhJDRpE8ksRCC3kDPS2nsvDAal28sMgyeP8sPvfKvp5sa2UsH78WJmTzeZWcJfX2C2ba3
xwRMB5LaaVrQZlhj9xjum0MfDpIS1hJI6p5CHZ8w&scope=searchadsorg'

Request header

Description

Host

Required. appleid.apple.com

Content-Type

Required. application/x-www-form-urlencoded

Request parameter

Type

Description

client_id

String

Required. You receive your clientId when you upload a public key.

client_secret

String

Required. The client secret is a JWT that you create and sign with your private key.

grant_type

String

Required. The method to request authorization and get an access token.

The value is client_credentials.

scope

String

Required. Defines the access permissions you request from the user, and limits the authorization of the access token you receive.

The value is searchadsorg.

返回值:

{
"access_token":"eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIiwia2lkIjpudWxsfQ
..lXm332TFi0u2E9YZ.bVVBvsjcavoQbBnQVeDiqEzmUIlaH9zLKY6rl36A_TD8wvgvWxp
yBXMQuhs-qWG_dxQ5nfuJEIxOp8bIndfLE_4a3AiYtW0BsppO3vkWxMe0HWnzglkFbKUHU
3PaJbLHpimmnLvQr44wUAeNcv1LmUPaSWT4pfaBzv3dMe3PNHJJCLVLfzNlWTmPxViIivQ
t3xyiQ9laBO6qIQiKs9zX7KE3holGpJ-Wvo39U6ZmGs7uK9BoNBPaFtd_q914mb9ChHAKc
QaxF3Gadtu_Z5rYFg.vD0iQuRwHGYVnDy27qexCw",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "searchadsorg"
}

6. 调用接口携带accesstoken和orgId

  • 获取 orgid:

(1)通过 Apple 页面拿到 orgId;


 

(2)通过 API 的方式拿到 orgId;

curl "https://api.searchads.apple.com/api/v3/acls

" \-H "Authorization: Bearer {access_token}" \

  • 通过accesstoken和orgId访问所有接口
curl "https://api.searchads.apple.com/api/v4/campaigns" \
-H "Authorization: Bearer {access_token}" \
-H "X-AP-Context: orgId={orgId}"

这篇关于asa(苹果Apple Search Ads平台)授权调用接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/257847

相关文章

springboot家政服务管理平台 LW +PPT+源码+讲解

3系统的可行性研究及需求分析 3.1可行性研究 3.1.1技术可行性分析 经过大学四年的学习,已经掌握了JAVA、Mysql数据库等方面的编程技巧和方法,对于这些技术该有的软硬件配置也是齐全的,能够满足开发的需要。 本家政服务管理平台采用的是Mysql作为数据库,可以绝对地保证用户数据的安全;可以与Mysql数据库进行无缝连接。 所以,家政服务管理平台在技术上是可以实施的。 3.1

比较学习难度:Adobe Illustrator、Photoshop和新兴在线设计平台

从入门设计开始,几乎没有人不知道 Adobe 公司两大设计软件:Adobe Illustrator和 Photoshop。虽然AI和PS很有名,有一定设计经验的设计师可以在早期探索和使用后大致了解AI和PS的区别,但似乎很少有人会系统地比较AI和PS。目前,设计软件功能多样,轻量级和网页设计软件已成为许多设计师的需求。对于初学者来说,一篇有针对性的AI和PS比较总结文章具有非常重要的指导意义。毕竟

探索Elastic Search:强大的开源搜索引擎,详解及使用

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 引入 全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选,相信大家多多少少的都听说过它。它可以快速地储存、搜索和分析海量数据。就连维基百科、Stack Overflow、

智慧环保一体化平台登录

据悉,在当今这个数字化、智能化的时代,环境保护工作也需要与时俱进,不断创新。朗观视觉智慧环保一体化平台应运而生,它利用先进的信息技术手段,为环保工作提供了更加便捷、高效的管理方式,成为推动绿色发展的重要力量。 一、智慧环保一体化平台的诞生背景 随着工业化进程的加快,环境污染问题日益严重,传统的环保管理模式已经难以满足现代社会的需求。为了提高环保工作的效率和质量,智慧环保一体化平台应运而

vue3项目将所有访问后端springboot的接口统一管理带跨域

vue3项目将所有访问后端springboot的接口统一管理带跨域 一、前言1.安装Axios2.创建Axios实例3.创建API服务文件4.在组件中使用API服务 二、跨域三、总结 一、前言 在Vue 3项目中,统一管理所有访问后端Spring Boot接口的最佳实践是创建一个专门的API服务层。这可以让你的代码更加模块化、可维护和集中管理。你可以使用Axios库作为HTT

ScrollView 非手动调用的方法

1. /**  *  非人为的时候调用这个方法  *  *  @param scrollView <#scrollView description#>  */ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {           } 2.判断控制器的view是否加载过 [willShowVC

苹果剪切板,涨姿势了UIPasteboard

有时候我们可能需要复制UILabel上的文本,或者UIImageView的图片,而UILabel和UIImageView默认是不响应Touch事件的,也无法复制,那么我们就需要自己实现一个可复制的UILabel。新添加一个类继承自UILabel: [objc]  view plain copy print ? @interface UICopyLabel : UILa

Apple - Media Playback Programming Guide

本文翻译整理自:Media Playback Programming Guide(Updated: 2018-01-16 https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/Introduction

基于Spring Boot构建淘客返利平台

基于Spring Boot构建淘客返利平台 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将讨论如何基于Spring Boot构建一个淘客返利平台。 淘客返利平台通过整合各种电商平台的商品信息,提供给用户查询和返利功能,从而实现流量变现。以下是实现一个简单的淘客返利平台的步骤。 1. 项目初始化 首先,使用Spri

IPD推行成功的核心要素(十一)技术规划与平台规划促进公司战略成功

随着外部大环境的影响,各企业仅有良好的愿望是不够的。预测并顺应新兴市场和技术的变化,变危机为转机,不断推出强大的产品才是一个公司持续繁荣的根本保障。而高效的产品开发往往是基于某些关键技术,针对市场推出的一个或几个产品系列,这些产品系列通常共用一些产品平台,共用一种或者几种关键技术。当一家企业进入了平稳发展期,已经建立了较为完善的管理制度和产品开发流程,但是依然认为竞争对手是那样强大,那样不可战胜。