ios - 谷歌自定义搜索 : 403 error in iOS

标签 ios google-api search-engine google-custom-search

Google 自定义搜索从我的 iPhone 7.1 应用返回这个 403 错误。这是在模拟器中运行时的响应:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."
   }
  ],
  "code": 403,
  "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."
 }
}

下面的步骤有错误吗?我想建立一个特定于 iOS 应用程序的有效 CSE 设置过程。希望每一步的屏幕截图都能有所帮助,不会造成混淆!

  1. https://www.google.com/cse/ 创建自定义搜索引擎 (CSE)

  2. 从设置 -> 基础,获取搜索引擎 ID。 CSE search engine ID

  3. https://developers.google.com/apis-explorer/#p/customsearch/v1/search.cse.list 测试 CSE

    • 将“q”设置为任何测试查询词(“foo bar”),将“cx”设置为第 2 步中的搜索引擎 ID。
    • 按“执行”并接收您的搜索结果。他们工作。 Google 还提供了这个 URL,我们将在第 8 步的 Xcode 中重新使用它。 Google API Explorer test results
  4. 要为我们自己获取用于 ID 和计费目的的 key ,请在 https://console.developers.google.com/ 创建一个新项目

  5. 在 API 和授权下 -> API -> 启用“自定义搜索 API”。没有启用其他 API。 Developer Console APIs

  6. 在 APIs & auth -> Credentials -> 创建一个新的 iOS key 。 Developer Console Credentials 注意:我还尝试了浏览器键,搜索结果返回“错误 400:无效值”。因为我在 iOS 上,所以我返回了 iOS key ,错误似乎不那么严重。

  7. 将来自 Xcode 的包标识符添加到此 key 。 Xcode bundle identifier

  8. 在 Xcode 中向第 3 步中的 URL 发出 GET 请求。将 {YOUR_API_KEY} 替换为第 6-7 步中您凭据中的 key 。

GET request in Xcode

NSData *response 包含上面显示的错误 403。感谢您对问题的任何想法!

最佳答案

它缺少 header 字段 X-Ios-Bundle-Identifier

NSString *bundleID = @"com.yourCompany.yourApp";
//...define request as above
[request setHTTPMethod:@"GET"];
[request setValue:bundleID forHTTPHeaderField:@"X-Ios-Bundle-Identifier"];

关于ios - 谷歌自定义搜索 : 403 error in iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24212412/

相关文章:

ios - 我应该释放原始 C 数组中的对象吗

ios - 如何更改/设置/管理 uicollectionview 中各部分之间的边框?

node.js - 使用 api (nodejs) 更新 Google 表格时出错

php - 如何在谷歌中编辑搜索结果?

ios - UIRefreshControl 精确内容偏移触发高度

ios - 在标签之后对齐 imageview

javascript - 使用 google maps api v3 自定义信息窗口框?

api - 使用 API 从关联的 Google 用户 ID 获取 YouTube channel ID

seo - 搜索引擎如何将结构化数据与页面内容进行交叉检查?

php - 如何避免某些页面被搜索引擎收录?