首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZOHO CRM未收到刷新令牌响应

ZOHO CRM未收到刷新令牌响应
EN

Stack Overflow用户
提问于 2019-07-15 15:31:54
回答 1查看 748关注 0票数 1

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

代码语言:javascript
复制
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来获取刷新和访问令牌。

代码语言:javascript
复制
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()
}

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

代码语言:javascript
复制
{
    "access_token": "1000.2......",
    "expires_in_sec": 3600,
    "api_domain": "https://www.zohoapis.in",
    "token_type": "Bearer",
    "expires_in": 3600000
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-02 21:42:12

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

代码语言:javascript
复制
access_type=offline&prompt=consent

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57035108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档