【问题标题】:using postman to access firebase REST API使用邮递员访问 Firebase REST API
【发布时间】:2016-12-04 08:23:07
【问题描述】:

我正在尝试使用邮递员对 firebase 进行 REST API 调用。当我的安全规则是允许所有用户(包括未经授权的用户)时,我已经设法从 firebase 读取数据。

但是当我使用这条规则时:

{"rules":{".read": "auth != null", ".write": "auth != null"}}

我从邮递员那里收到“错误”:“权限被拒绝”。 我为谷歌的 web oauth2.0 客户端做了请求令牌,并取回了授权码令牌。

我尝试在 URL 和标头中使用令牌,尝试使用 GET 和 POST 请求,但仍然被拒绝。

请帮忙。 提前致谢

【问题讨论】:

  • 您需要一个具有适当范围的访问令牌,授权码只是用于获取 OAuth 2.0 访问令牌的中间凭证。一旦你有了它,你可以在标题中将它作为Authorization: Bearer <the_token>
  • 权限仍然被拒绝。我使用的范围是 googleapis.com/auth/firebase 我如何测试令牌是否有效。我不确定问题出在哪里: 1. google creds? 2. firebase 身份验证/规则? 3.邮递员的oauth?
  • 在下面的帖子中查看我更新的 cmets :)

标签: rest firebase firebase-realtime-database postman


【解决方案1】:

上面的答案对我不起作用。

对我有用的是

Project Settings(左上角齿轮)-> Service Accounts(最右侧选项卡)-> Database Secrets(左侧菜单)- > 向下滚动,将鼠标悬停在项目符号上并点击显示

将此作为身份验证密钥,即.../mycollection.json?auth=HERE

【讨论】:

  • .json 是 Rest Api 必须的
  • 应该接受答案,因为这是唯一有效的解决方案!
【解决方案2】:

对我来说,它是这样工作的:

https://your-database-url/users.json?auth=YOUR_AUTH_KEY

在哪里可以得到这个 AUTH_KEY?

你从你的Project Settings -> Database -> Secret Key得到这个密钥

【讨论】:

  • 项目设置 -> 数据库 -> 密钥?没有这样的路径
  • 目前,通过单击在您的 Firebase 控制台中访问密钥:项目设置 -> 服务帐户(选项卡)-> 数据库秘密 然后将鼠标悬停在密钥上,将显示“显示”按钮。点击这个然后你就可以看到了。另请注意,您可以通过单击左侧菜单中的“数据库”项从控制台中找到您的数据库 URL。
  • 还可以尝试在 Firebase 控制台中的 Database->Rules 下使用模拟器。它可以帮助您建立一个有效的经过身份验证的请求,然后您可以将其复制到邮递员。
  • @andreEvangelista 是的,它起作用了,但是 firebase 文档说数据库机密现在已经贬值了,所以我们还应该使用它吗?我使用的是 c#,并且没有可用的官方 sdk,所以我必须使用 restapi 并使用解决方法,你的建议。
【解决方案3】:

试试这样的

https://your-database-url/users.json?auth=YOUR_AUTH_KEY

Respone 是您的 USERS 节点的 JSON

【讨论】:

  • 权限仍然被拒绝。我使用的范围是googleapis.com/auth/firebase
  • 更正:所以我使用 access_token 参数而不是 auth。这就是为什么我一直被拒绝许可。将其更改为 AUTH 后,我在使用返回的 access_token 时得到了"error": "Could not parse auth token."。根据 firebase 文档,您可以使用经过身份验证的令牌或 firebase 的应用程序密钥访问它。使用后者作为我的 AUTH 值,我设法获取了数据。但是使用经过身份验证的令牌仍然会导致错误
  • 同样的问题
【解决方案4】:

我创建了一个 Postman 预请求脚本来帮助创建 Authentication: Bearer JWT。使用 Firebase Auth 测试 API 时应该节省大量复制粘贴。 https://gist.github.com/moneal/af2d988a770c3957df11e3360af62635

发布时的脚本副本:

/**
 * This script expects the global variables 'refresh_token' and 'firebase_api_key' to be set. 'firebase_api_key' can be found
 * in the Firebase console under project settings then 'Web API Key'.
 * 'refresh_token' as to be gathered from watching the network requests to https://securetoken.googleapis.com/v1/token from 
 * your Firebase app, look for the formdata values
 * 
 * If all the data is found it makes a request to get a new token and sets a 'auth_jwt' environment variable and updates the 
 * global 'refresh_token'.
 * 
 * Requests that need authentication should have a header with a key of 'Authentication' and value of '{{auth_jwt}}'
 *
 * Currently the nested assertions silently fail, I don't know why.
 */
pm.expect(pm.globals.has('refresh_token')).to.be.true;
pm.expect(pm.globals.has('firebase_api_key')).to.be.true;

var sdk = require('postman-collection'),
  tokenRequest = new sdk.Request({
    url: 'https://securetoken.googleapis.com/v1/token',
    method: 'POST',
    body: {
      mode: 'urlencoded',
      urlencoded: [{
          type: 'text',
          key: 'key',
          value: pm.globals.get('firebase_api_key')
        },
        {
          type: 'text',
          key: 'grant_type',
          value: 'refresh_token'
        },
        {
          type: 'text',
          key: 'refresh_token',
          value: pm.globals.get('refresh_token')
        },
      ]
    }
  });

pm.sendRequest(tokenRequest, function(err, response) {

  pm.test('request for access token was ok', function() {
    pm.expect(response).to.be.ok();
  });

  const json = response.json();
  pm.expect(json).to.an('object');

  pm.test('response json has needed properties', function() {

    pm.expect(json).to.have.own.property('access_token');
    pm.expect(json).to.have.own.property('token_type');
    pm.expect(json).to.have.own.property('refresh_token');

    const accessToken = json.access_token;
    const tokenType = json.token_type;
    const refreshToken = json.refresh_token;

    pm.environment.set('auth_jwt', tokenType + ' ' + accessToken);
    pm.globals.set('refresh_token', refreshToken);

  });

});

【讨论】:

    【解决方案5】:

    注意:添加此答案,因为此处列出的所有选项都已弃用或不起作用(主要是由于缺少步骤)。

    使用 Postman 的最佳方法是使用 Google OAuth2 access tokens。提供的链接完整描述,但我添加了快速步骤。

    第一步:下载Service-Accounts.json

    第 2 步:在 Java 中生成访问令牌(提供链接描述的其他语言支持)

    • 确保包含此依赖项:
    implementation 'com.google.api-client:google-api-client:1.25.0'
    

    <dependency>
       <groupId>com.google.api-client</groupId>
       <artifactId>google-api-client</artifactId>
       <version>1.25.0</version>
     </dependency>
    
    • 运行此代码生成令牌(从google的javadocs复制)
       // Load the service account key JSON file
         FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
    
         GoogleCredential scoped = GoogleCredential
         .fromStream(serviceAccount)
         .createScoped(
             Arrays.asList(
               "https://www.googleapis.com/auth/firebase.database",
               "https://www.googleapis.com/auth/userinfo.email"
             )
         );
         // Use the Google credential to generate an access token
         scoped.refreshToken();
         String token = scoped.getAccessToken();
         System.out.println(token);
    
    

    第 3 步:在 Postman 中使用令牌

    【讨论】:

      【解决方案6】:

      通过 Postman 获取数据非常简单: 这是我的做法

      1 您的数据库网址

      https://YOUR_PROJECT_URL.firebaseio.com/YOUR_STRUCTURE/CLASS.json

      2 在标头中添加 API 密钥作为身份验证

      auth = API_KEY 的值

      示例:

      【讨论】:

      • 什么是 config.json ?你从哪里得到这门课的
      • 我无法在邮递员中获取数据。你能告诉我url中的data和config.json是什么吗?
      • 哦,请不要继续config.json,因为这只是您的 URL 的 URL 端点。该 URL 只是 URL 的一个示例。这里config 是我要访问的类的名称。
      • 它说,{ "error" : "Permission denied" },我已经添加了 Header auth
      • @N.K如果您正在这样做,请使用您的 API 文档确认您没有遗漏正确的格式,您必须在该格式中准备 API 请求参数和标头参数。
      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2022-10-01
      • 2020-07-13
      相关资源
      最近更新 更多