簡體   English   中英

使用 postman 訪問 firebase REST API

[英]using postman to access firebase REST API

我正在嘗試使用 postman 來執行 REST API 對 firebase 的調用。當我的安全規則是允許所有用戶(包括未經授權的用戶)時,我已經設法從 firebase 讀取。

但是當我使用這條規則時:

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

我從 postman 收到“錯誤”:“權限被拒絕”。我為谷歌的 web oauth2.0 客戶端做了請求令牌,並取回了 authorization_code 令牌。

我嘗試在 URL 和 header 中使用令牌,嘗試使用 GET 和 POST 請求,但仍然被拒絕。

請幫忙。 提前致謝

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

對我有用的是什么

項目設置 (左上角齒輪) - > 服務帳戶 (最右邊的選項卡) - > 數據庫秘密 (左側菜單) - >向下滾動,將鼠標懸停在bulltets上並單擊Show

使用它作為auth鍵,即.../mycollection.json?auth=HERE

對我來說它的工作方式如下:

HTTPS://your-database-url/users.json AUTH = YOUR_AUTH_KEY

你在哪里可以得到這個AUTH_KEY?

您可以從Project Settings -> Database -> Secret Key獲取此密鑰

嘗試這樣的事情

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

Respone是您的USERS節點的JSON

我創建了一個Postman預請求腳本,用於幫助創建身份驗證:承載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);

  });

});

通過Postman獲取數據非常簡單:我就是這樣做的

1您的數據庫URL

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

2在標頭中添加API密鑰作為auth

auth = API_KEY的值

例:

1

注意:添加此答案,因為此處列出的所有選項已棄用或不起作用(主要是由於缺少步驟)。

使其與Postman一起使用的最佳方法是使用Google OAuth2訪問令牌 提供的鏈接全文描述,但我添加了快速步驟。

第1步:下載Service-Accounts.json

answer_img_1

第2步:在Java中生成Access令牌(提供的鏈接描述了支持其他語言)

  • 確保包含此依賴項:
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>
  • 運行此代碼以生成令牌(從谷歌的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中使用令牌

發布男子Oauth 2令牌

我們可以將 Firebase 和 postman 用於 rest API

我們如何在postman中使用Firebase?

  1. 復制你的 Firebase 數據庫 URL
  2. 粘貼到你的 postman URL
  3. Add.json 最后如圖所示
  4. 然后玩你的 firebase 數據庫並制作 REST API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM