signUp with Google、Amazon、Apple

本文详细介绍了Google、Amazon和Apple的OAuth2.0授权过程,包括搭建开发环境、请求code、获取access_token、刷新token及获取用户信息的步骤。对于每个平台,都提供了具体的请求URL和所需参数,帮助开发者理解并实现第三方登录功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Google

1.搭建开发环境

https://console.cloud.google.com/https://console.cloud.google.com/配置一个project,得到clientID、clientSecret,填写redirect_uri:

2.请求 code

需要的参数:

client_id

redirect_uri

response_type

scope

其中,client_id是在第一步中创建凭据的时候得到的,向Google 授权服务器发送如下信息则可以获取到code

https://accounts.google.com/o/oauth2/auth?client_id=XXX&redirect_uri=XXX&scope=email&response_type=code

使用浏览器访问到上面的网址,点击确认授权以后,则可以获取一段

code 需要url decode!!!

response_type = token 可直接获取access_token

 3.请求access token

通过向Google发送post 请求,来获得access token

https://accounts.google.com/o/oauth2/tokenhttps://accounts.google.com/o/oauth2/token

需要的参数:POST

code = XXX

client_id = XXX

cilent_secret = XXX

redirect_uri = XXX

grant_type = authorization_code

如果成功以后,我们会收到JSON格式的Response:

{
    "access_token": "xxx",
    "expires_in": 3600,
    "scope": "openid https://www.googleapis.com/auth/userinfo.email",
    "token_type": "Bearer",
    "id_token": "xxx"
}

4.使用refresh token来刷新access token

当access_token失效的时候,我们需要通过refresh_token来重新获取。发送HTTP POST请求:

https://www.googleapis.com/oauth2/tokenhttps://www.googleapis.com/oauth2/v4/token

需要的参数:

refresh_token = XX

client_id = XX

client_secret = XX

grant_type = XX

发送请求的格式如下:

POST

请求成功之后,会多的JOSN格式的Response,如下:

{ 
"access_token": "xxx", 
"token_type": "Bearer", 
"expires_in": 3600 
}

因为服务器对refresh_token的请求有限制,如果过多的请求会导致请求失败的情况。所以需要长期保存refresh_token,只要refresh_token没有失效,就没有必要多次请求。

5. 获取profile

https://openidconnect.googleapis.com/v1/userinfohttps://openidconnect.googleapis.com/v1/userinfo

Header :

Authorization = Bearer ya29.****

{ "sub": "****", 
"picture": "https://lh3.googleusercontent.com/****", 
"email": "****", 
"email_verified": true 
}

Amazon

官方获取code 文档地址:

https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.htmlhttps://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html

1.搭建开发环境

客户端ID:amzn1.xx

客户端密钥:xxxx

2.请求code

https://www.amazon.com/ap/oa?client_id=***&scope=profile&response_type=code&state=email&redirect_uri=http://localhost:8080/

 3.请求access_token

 POST https://api.amazon.com/auth/o2/token

参数 x-www-form-urlencoded

 grant_type: authorization_code

code: xxx

client_id: xxx

client_secret: xxx

redirect_uri: localhost:8080

返回:

{
    "access_token": "",
    "refresh_token": "",
    "token_type": "bearer",
    "expires_in": 3600
}

 4.刷新token

POST https://api.amazon.com/auth/o2/token

 参数 x-www-form-urlencoded

 grant_type: refresh_token

client_id: xxx

client_secret: xxx

refresh_token: xxx

5.获取profile

GET https://api.amazon.com/user/profile?access_token=xxx

{
    "user_id": "",
    "name": "",
    "email": ""
}

 Apple

Apple Developer Documentationhttps://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple

 1.配置项

client_id: xx

client_secret: jwt token形式的

Team ID: xx

key ID: xx

P8 key 私钥:xx

redirect_uri: xx

2.请求code 

https://appleid.apple.com/auth/authorize?response_mode=query&state=xx&response_type=code&client_id=xx&redirect_uri=xx

 3.获取token

POST https://appleid.apple.com/auth/token

 参数 x-www-form-urlencoded

 grant_type: authorization_code

code: xxx

client_id: xxx

client_secret: xxx

redirect_uri: localhost:8080

client_secret 就是自己用脚本生成的jwt token 可参考Apple Developer Documentation

{
    "access_token": "",
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": "",
    "id_token": ""
}

用户信息通过id_token解析得出

### Sign in with Apple Entitlement Configuration For applications utilizing **Sign in with Apple**, ensuring proper entitlements setup is crucial for functionality. In the project settings within Xcode, navigate to the "Signing & Capabilities" tab. Add the `Sign in with Apple` capability by clicking the '+' button at the top-left corner of this section[^1]. This action automatically configures necessary entitlements such as `com.apple.developer.applesignin`. When configuring release builds, ensure that the correct provisioning profile supports all required capabilities including Sign in with Apple. Set the Release build configuration's Code Signing Identity to iOS Distribution, and set the Release build configuration's Provisioning Profile build setting to your App Store distribution provisioning profile. If encountering issues related to missing certificates or profiles during development or deployment phases, restarting Xcode can help apply recent changes made to code signing configurations[^2]. Additionally, verify whether the desired provisioning profile exists within Xcode’s library; otherwise, transferring the corresponding signing certificate might be necessary according to official documentation guidelines[^3]. ### Troubleshooting Common Issues A common issue arises when developers face errors indicating mismatches between their selected provisioning profile and available signing identities. If an appropriate signing identity cannot be found locally, consider exporting it from another machine where it was initially generated following documented procedures. Another frequent problem involves misconfiguration of entitlements which prevents successful authentication via Sign in with Apple services. Double-check both local and server-side configurations against Apple’s latest requirements published online periodically updated based on platform updates. Lastly, always keep track of any deprecations announced by Apple regarding APIs used alongside Sign in with Apple features since these may affect compatibility over time without notice. ```swift // Example Swift code snippet demonstrating how to handle sign-in completion. import AuthenticationServices class SignInWithAppleHandler: NSObject { func startSignIn() { let request = ASAuthorizationAppleIDProvider().createRequest() let controller = ASAuthorizationController(authorizationRequests: [request]) controller.delegate = self controller.presentationContextProvider = self controller.performRequests() } } extension SignInWithAppleHandler: ASAuthorizationControllerDelegate { func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { // Handle error here... } func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { // Process user info upon success... } } ``` --related questions-- 1. How do I add custom parameters while initiating Sign in with Apple? 2. What steps should one follow to troubleshoot failed login attempts through Sign in with Apple? 3. Can multiple apps share the same Sign in with Apple credentials across different platforms like macOS and iOS? 4. Is there a way to test Sign in with Apple functionalities before submitting my app for review?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值