storm小店长 2015-05-06 02:06 采纳率: 0%
浏览 859

ios 新浪微博API 如何获取指定人微博列表 不登陆

        我有个需求是获取 新浪微博指定人的博客信息 不登陆情况下 已查阅新浪微博API,由于对他的这个授权和登陆的意思有点不太明白,所以也不知道用哪个接口做这个事,或者能不能做这个事
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 19:47
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    新浪的微博API主要提供的是微博用户的个人信息,包括用户ID、昵称、头像等。而获取博主的博客信息,这通常涉及到访问博主的个人主页或者是查看博主发布的文章。

    以下是一个基本的例子,展示如何使用新浪的微博API来获取博主的个人资料:

    import Foundation
    
    let access_token = "你的access_token"
    let user_id: String = "博主的ID"
    
    func getBlogList() -> [String] {
        let url = "https://api.weibo.com/2/users/show.json?access_token=\(access_token)&id=\(user_id)"
        
        var result = [String]()
        URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
            guard let data = data else {
                print("Error: \(error?.localizedDescription ?? "无错误")")
                return
            }
            
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                if let jsonArray = json as? [[String: Any]] {
                    for item in jsonArray {
                        if let name = item["name"] as? String, name == "weibo" {
                            let blogList = item["blog_list"] as? [[String: Any]]
                            for blog in blogList! {
                                let blogId = blog["id"] as? String
                                result.append(blogId)
                            }
                        }
                    }
                }
            } catch {
                print("Error: \(error.localizedDescription)")
            }
        }.resume()
    }
    
    getBlogList()
    

    注意:上述代码中,你需要替换your_access_token为你自己的新浪微博API的Access Token,以及user_id为你要获取的博主的ID。

    此外,这个示例没有处理可能发生的网络错误或超时问题。在实际使用中,你可能还需要添加适当的异常处理逻辑。

    评论

报告相同问题?