gpt4 book ai didi

swift - ZOHO CRM 未获得刷新 token 作为响应

转载 作者:行者123 更新时间:2023-12-05 00:47:28 24 4
gpt4 key购买 nike

我正在 iOSandroid 中开发应用程序,因为我正在集成 ZOHO CRM。我使用 OAuth2.0 进行身份验证,之后我使用 REST API 来获取“刷新 token ”,但我只得到“访问 token ”。有下面的代码来获取 token 。如何获取刷新 token ?

self.getCodeFromCRM(client_id: Client_ID,
clientSecret: secID,
authURL: "https://accounts.zoho.in/oauth/v2/auth",
accessURL: "offline",
responseType: "code",
callBackURL: "zohoapp://",
scope: "ZohoCRM.modules.contacts.all",//ZohoCRM.users.ALL
state: "code")

获取代码后调用此 API 获取刷新和访问 token 。

func getZohoReferenceToken()
{
let headers = [
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "PostmanRuntime/7.13.0",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "88ebde59-240a-4e52-8ff9-bb7384eba0dd,9a1d5ea1-a5c0-490e-b3b5-1884e335ef86",
"Host": "accounts.zoho.in",
"accept-encoding": "gzip, deflate",
"content-length": "254",
"Connection": "keep-alive",
"cache-control": "no-cache"
]

let postData = NSMutableData(data: "client_id=\(Client_ID)".data(using: String.Encoding.utf8)!)
postData.append("&client_secret=\(secID)".data(using: String.Encoding.utf8)!)
postData.append("&redirect_uri=zohoapp://".data(using: String.Encoding.utf8)!)
postData.append("&code=\(code)".data(using: String.Encoding.utf8)!)
postData.append("&grant_type=authorization_code".data(using: String.Encoding.utf8)!)
postData.append("&prompt=consent".data(using: String.Encoding.utf8)!)

let request = NSMutableURLRequest(url: NSURL(string: "https://accounts.zoho.in/oauth/v2/token")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error!)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse!)

do {
//create json object from data
if let json:NSDictionary = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
{
// UserDefaults.standard.set(json.value(forKey: "access_token") as! String, forKey: "ZOHO_access")
print(json)
let access:String = ""//json.value(forKey: "access_token") as! String;
let ref:String = ""//json.value(forKey: "refresh_token") as! String

DispatchQueue.main.asyncAfter(deadline: .now() + 3.0, execute: {
self.displayAlert(appname: "ZOHO", accessToken: access, referenseToken: ref)
})
}
} catch let error {
print(error.localizedDescription)
}
}
})

dataTask.resume()
}

响应:您可以在下面的响应中看到我没有获得刷新 token 。请帮助我如何获得刷新 token ?

{
"access_token": "1000.2......",
"expires_in_sec": 3600,
"api_domain": "https://www.zohoapis.in",
"token_type": "Bearer",
"expires_in": 3600000
}

最佳答案

我遇到了同样的问题,但最终在 Mail API 的文档中找到了答案。要强制刷新 token ,您需要向初始 Post 添加两个附加参数:

access_type=offline&prompt=consent

这可确保您获得刷新 token 和访问 token 。

关于swift - ZOHO CRM 未获得刷新 token 作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57035108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com