如何在颤振中使用密钥请求访问请求令牌?

时间:2020-12-21 03:34:37

标签: android api flutter visual-studio-code flutter-dependencies

如何在flutter中使用Secret Key请求访问Request Token

大家好。这是我第一次问一个小问题,可能是我没有解决的难题。 几个月后,我被困在案例请求访问令牌和颤振中的秘密密钥。

<块引用>

对不起,我的英语不好。

问题来了

<块引用>

无效参数(ShredPreferencesManager 类型的对象未在 GetIt 中注册。

源文档

1.颤振医生 -v

[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Version 10.0.18363.1256], locale en-US)
    • Flutter version 1.22.5 at C:\src\flutter
    • Framework revision 7891006299 (10 days ago), 2020-12-10 11:54:40 -0800
    • Engine revision ae90085a84
    • Dart version 2.10.4

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\User\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin installed
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.52.1)
    • VS Code at C:\Users\User\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.16.0

[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.

2. API 文档

<块引用>

身份验证(请求令牌)

<头>
网址 /get-token
<头>
方法 POST

参数 |否 |类型 |参数名称 |描述 |强制性 | | -- | ------ | ----------- | ---- | ----------| | 1 |字符串 |秘密| ---- |是 |

回应 |否 |类型 |回复姓名 | | ---- | ------- | -------------- | | 1 |整数 | api_status | | 2 |字符串 | api_message | | 3 |对象 |数据| | 3.1 |字符串 | access_token | | 3.2 |整数 |到期|

3.邮递员

<块引用>

sikompak.id/api/get-token

<头>
KEY 价值
秘密 446ddeec4414b74b4f3efaa24736d489
Body :
{
    "api_status": 1,
    "api_message": "success",
    "data": {
        "access_token": "k9vFyipM4g9Ipyz0",
        "expiry": 1608690395
    }
}

4. api服务代码

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:SIKOMPAK/models/data/KecamatanParent.dart';
import 'package:SIKOMPAK/models/data/KelurahanParent.dart';
import 'package:SIKOMPAK/models/data/KotaParent.dart';
import 'package:SIKOMPAK/models/data/ProvinsiParent.dart';
import 'package:SIKOMPAK/models/data/SektorParent.dart';
import 'package:SIKOMPAK/models/data/SubsektorParent.dart';
import 'package:SIKOMPAK/models/data/user.dart';


class ApiService{
  final String baseUrl = "https://www.sikompak.id/api";
  HttpClient client = new HttpClient();

  Future<SektorParent> getSektor() async {
    client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);

    HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sektor_usaha"));
    //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha?sektor_usaha_id=1"));

    request.headers.set('content-type', 'application/json');
    // request.headers.set('Authorization', '446ddeec4414b74b4f3efaa24736d489');
    // request.headers.set('Authorization', SharedPreferences.get("access_token"));

    HttpClientResponse response = await request.close();

    if (response.statusCode == 200) {

      print("StatusCode: "+response.statusCode.toString());
      SektorParent sektorParent;
      await response.transform(utf8.decoder).join().then((body) {

        print("body: "+body.toString());
        final parsed = json.decode(body).cast<String, dynamic>();
        print("parsed: "+parsed.toString());

        sektorParent = SektorParent.fromJson(parsed);
        print("sektorParent: "+sektorParent.apiMessage);

      });

      print("sektorParent: "+sektorParent.apiMessage);
      return sektorParent;

    } else {
      print("StatusCode: "+response.statusCode.toString());

      if(response.statusCode.toString() == "403") {
        getTokenWithCallback(this.getSektor); 
      }

      // If the server did not return a 200 OK response,
      // then throw an exception.
      throw Exception('Failed to load album');
    }
  }

  getTokenWithCallback(callback) async {
    client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);

    HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/get-token"));
    //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha?sektor_usaha_id=1"));

    request.headers.set('content-type', 'application/json');
    // request.headers.set('Authorization', '446ddeec4414b74b4f3efaa24736d489');
    // request.headers.set('Authorization', SharedPreferences.get("accessToken"));
    HttpClientResponse response = await request.close();

    if (response.statusCode == 200) {
      //save token to shared preferences

      callback();
    } else {
      print("StatusCode: "+response.statusCode.toString());

      if(response.statusCode.toString() == "403") {
        // requestGetToken(this.getSektor); 
        requestgetToken(this.getSektor); 
              }
        
              // If the server did not return a 200 OK response,
              // then throw an exception.
              throw Exception('Failed to load album');
            }
          }
        
          getToken() async {
            client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
        
            HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/get-token"));
            //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha?sektor_usaha_id=1"));
        
            request.headers.set('content-type', 'application/json');
            request.headers.set('Authorization', '446ddeec4414b74b4f3efaa24736d489');
            HttpClientResponse response = await request.close();
        
            if (response.statusCode == 200) {
              //save token to shared preferences
            } else {
              print("StatusCode: "+response.statusCode.toString());
        
              if(response.statusCode.toString() == "400") {
                //requestGetToken(this.getSektor); 
                requestGetToken(this.getSektor); 
                }
              // If the server did not return a 200 OK response,
              // then throw an exception.
              throw Exception('Failed to load album');
              }
            }
                                    
            Future<SubSektorParent> getSubSektor(idSubSektor) async {
              client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
                                    
              HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha"));
              //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/list_kota?provinsi_id=15"));
                                    
              request.headers.set('content-type', 'application/json');
              request.headers.set('Authorization', 'Bearer 446ddeec4414b74b4f3efaa24736d489');
              //request.headers.set('Authorization', 'Bearer tokenna');
              HttpClientResponse response = await request.close();
                                    
              if (response.statusCode == 200) {
                print("StatusCode: "+response.statusCode.toString());
                SubSektorParent subsektorParent;
                await response.transform(utf8.decoder).join().then((body) {
                  print("body: "+body.toString());
                  final parsed = json.decode(body).cast<String, dynamic>();
                  print("parsed: "+parsed.toString());
                                    
                  subsektorParent = SubSektorParent.fromJson(parsed);
                  print("subsektorParent: "+subsektorParent.apiMessage);
                  }
                );
                                    
                print("subsektorParent: "+subsektorParent.apiMessage);
                return subsektorParent;
                                    
              } else {
                print("StatusCode: "+response.statusCode.toString());
                // If the server did not return a 200 OK response,
                // then throw an exception.
                throw Exception('Failed to load album');
                }
              }
                                    
              Future<ProvinsiParent> getProvinsi() async {
                client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
                HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/list_provinsi"));
                                    
                request.headers.set('content-type', 'application/json');
                request.headers.set('Authorization', 'Bearer 446ddeec4414b74b4f3efaa24736d489');
                HttpClientResponse response = await request.close();
                                    
                if (response.statusCode == 200) {
                  print("StatusCode: "+response.statusCode.toString());
                  ProvinsiParent provinsiParent;
                  await response.transform(utf8.decoder).join().then((body) {
                                    
                  print("body: "+body.toString());
                  final parsed = json.decode(body).cast<String, dynamic>();
                  print("parsed: "+parsed.toString());
                                    
                  provinsiParent = ProvinsiParent.fromJson(parsed);
                  print("provinsiParent: "+provinsiParent.apiMessage);
                                    
                  }
                );
                                    
                print("provinsiParent: "+provinsiParent.apiMessage);
                return provinsiParent;
                                    
                } else {
                  print("StatusCode: "+response.statusCode.toString());
                  // If the server did not return a 200 OK response,
                  // then throw an exception.
                  throw Exception('Failed to load album');
                  }
                }
                                      
                Future<KotaParent> getKota(id) async {
                  client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
                                   
                  HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/list_kota"));
                  //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha?sektor_usaha_id=1"));
                                    
                  request.headers.set('content-type', 'application/json');
                  request.headers.set('Authorization', 'Bearer 446ddeec4414b74b4f3efaa24736d489');
                  HttpClientResponse response = await request.close();
                                    
                  if (response.statusCode == 200) {
                                    
                    print("StatusCode: "+response.statusCode.toString());
                    KotaParent kotaParent;
                    await response.transform(utf8.decoder).join().then((body) {
                                    
                    print("body: "+body.toString());
                    final parsed = json.decode(body).cast<String, dynamic>();
                    print("parsed: "+parsed.toString());
                                    
                    kotaParent = KotaParent.fromJson(parsed);
                    print("sektorParent: "+kotaParent.apiMessage);
                                    
                    });
                                    
                    print("kotaParent: "+kotaParent.apiMessage);
                    return kotaParent;
              
                  } else {
                    print("StatusCode: "+response.statusCode.toString());
                    // If the server did not return a 200 OK response,
                    // then throw an exception.
                    throw Exception('Failed to load album');
                  }
                }
              
                Future<KecamatanParent> getKecamatan(id) async {
                  client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
              
                  HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/list_kecamatan"+id));
                  //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha?sektor_usaha_id=1"));
              
                  request.headers.set('content-type', 'application/json');
                  request.headers.set('Authorization', 'Bearer 446ddeec4414b74b4f3efaa24736d489');
                  HttpClientResponse response = await request.close();
              
                  if (response.statusCode == 200) {
              
                    print("StatusCode: "+response.statusCode.toString());
                    KecamatanParent kecamatanParent;
                    await response.transform(utf8.decoder).join().then((body) {
              
                      print("body: "+body.toString());
                      final parsed = json.decode(body).cast<String, dynamic>();
                      print("parsed: "+parsed.toString());
              
                      kecamatanParent = KecamatanParent.fromJson(parsed);
                      print("kecamatanParent: "+kecamatanParent.apiMessage);
              
                    });
              
                    print("kecamatanParent: "+kecamatanParent.apiMessage);
                    return kecamatanParent;
              
                  } else {
                    print("StatusCode: "+response.statusCode.toString());
                    // If the server did not return a 200 OK response,
                    // then throw an exception.
                    throw Exception('Failed to load album');
                  }
                }
              
              Future<KelurahanParent> getKelurahan(id) async {
                  client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
              
                  HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/list_kelurahan"+id));
                  //HttpClientRequest request = await client.getUrl(Uri.parse("$baseUrl/sub_sektor_usaha?sektor_usaha_id=1"));
              
                  request.headers.set('content-type', 'application/json');
                  request.headers.set('Authorization', 'Bearer 446ddeec4414b74b4f3efaa24736d489');
                  HttpClientResponse response = await request.close();
              
                  if (response.statusCode == 200) {
              
                    print("StatusCode: "+response.statusCode.toString());
                    KelurahanParent kelurahanParent;
                    await response.transform(utf8.decoder).join().then((body) {
              
                      print("body: "+body.toString());
                      final parsed = json.decode(body).cast<String, dynamic>();
                      print("parsed: "+parsed.toString());
              
                      kelurahanParent = KelurahanParent.fromJson(parsed);
                      print("kelurahanParent: "+kelurahanParent.apiMessage);
              
                    });
              
                    print("kelurahanParent: "+kelurahanParent.apiMessage);
                    return kelurahanParent;
              
                  } else {
                    print("StatusCode: "+response.statusCode.toString());
                    // If the server did not return a 200 OK response,
                    // then throw an exception.
                    throw Exception('Failed to load album');
                  }
                }
              
                Future<bool> register(sektorId, subSektorId, userParam) async {
                  client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
              
                  HttpClientRequest request = await client.postUrl(Uri.parse("$baseUrl/post_pelaku_usaha"));
              
                  User user = userParam;
              
                  Map map = {
                    "nama": user.name,
                    "tempat_lahir": user.tempatLahir,
                    "tgl_lahir": user.tanggalLahir,
                    "jenis_kelamin": user.gender,
                    "no_hp": user.hp,
                    "email": user.email,
                    // "provinsi_id": provinsiId,
                    // "kota_id": kotaId,
                    // "kecamatan_id": kecamatanId,
                    // "desa_id": desaId,
                    "alamat": user.address,
                    "no_ktp": user.ktp,
                    "no_kk": user.kk,
                    "nama_usaha": user.namaUsaha,
                    "badan_usaha": user.badanUsaha,
                    "id": sektorId,
                    "sektor_usaha_id": subSektorId,
                    "no_izin_usaha" : user.noIzin,
                    "tgl_izin_usaha" : user.tglIzin,
                    "npwp": user.npwp,
                    "alamat_usaha": user.alamatUsaha,
                    "website": user.website,
                    "telp": user.notlpUsaha
                  };
              
                  request.headers.set('content-type', 'application/json');
                  request.add(utf8.encode(json.encode(map)));
              
                  request.headers.set('Authorization', 'Bearer 446ddeec4414b74b4f3efaa24736d489');
                  HttpClientResponse response = await request.close();
              
                  bool result;
                  if (response.statusCode == 200) {
                    print("StatusCode: "+response.statusCode.toString());
                    // SubSektorParent subsektorParent;
                    await response.transform(utf8.decoder).join().then((body) {
                      print("body: "+body.toString());
                      final parsed = json.decode(body).cast<String, dynamic>();
                      print("parsed: "+parsed.toString());
              
                      // subsektorParent = SubSektorParent.fromJson(parsed);
                      // print("subsektorParent: "+subsektorParent.apiMessage);
              
                      print("register sukses");
                      result = true;
                    });
              
                    // print("subsektorParent: "+subsektorParent.apiMessage);
                  } else {
                    print("register gagal");
                    result = false;
                    print("StatusCode: "+response.statusCode.toString());
                    // If the server did not return a 200 OK response,
                    // then throw an exception.
                    throw Exception('Failed to load album');
                  }
                  return result;
                }
                void requestGetToken(Future<SektorParent> Function() getSektor) {}
              }
              void requestgetToken(Future<SektorParent> Function() getSektor) {}

询问请求

我很高兴尽快了解我遇到的问题。谢谢大家~

0 个答案:

没有答案