首页 > 解决方案 > 使用 GmailApi 的正确方法是什么?

问题描述

我能够GmailApigoogleapi颤动中获得用户电子邮件的许可。但是我发现很难实现它。作为GmailApi一个包,我确信我不必从正常情况下获取用户信息和邮件,http所以我进行了一些研究,并遵循了一些教程和其他线程,googleapis这与使用包导入的标签有关

import 'package:googleapis/gmail/v1.dart'
  show
  Label, Profile, GmailApi, Filter, Message, UsersResourceApi, Thread, BatchDeleteMessagesRequest, BatchDeleteMessagesRequest, Draft, UsersMessagesAttachmentsResourceApi; 
  //tags I mentioned in the paragraph above.

我能够获得用户邮件权限,并将 httpClient 初始化为登录用户。

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn(
      scopes: [
          GmailApi.MailGoogleComScope,
          GmailApi.GmailComposeScope,
          GmailApi.MailGoogleComScope,
          GmailApi.GmailInsertScope,
          GmailApi.GmailLabelsScope,
          GmailApi.GmailMetadataScope,
          GmailApi.GmailModifyScope,
          GmailApi.GmailReadonlyScope,
          GmailApi.GmailSendScope,
          GmailApi.GmailSettingsSharingScope,
      ]
);


final httpClient = googleSignIn.currentUser.authHeaders;



class GmailApi{
  static const MailGoogleComScope = "https://mail.google.com/";
  static const GmailComposeScope = "https://www.googleapis.com/auth/gmail.compose";
  static const GmailInsertScope = "https://www.googleapis.com/auth/gmail.insert";
  static const GmailLabelsScope = "https://www.googleapis.com/auth/gmail.labels";
  static const GmailMetadataScope = "https://www.googleapis.com/auth/gmail.metadata";
  static const GmailModifyScope = "https://www.googleapis.com/auth/gmail.modify";
  static const GmailReadonlyScope = "https://www.googleapis.com/auth/gmail.readonly";
  static const GmailSendScope = "https://www.googleapis.com/auth/gmail.send";
  static const GmailSettingsBasicScope = "https://www.googleapis.com/auth/gmail.settings.basic";
  static const GmailSettingsSharingScope = "https://www.googleapis.com/auth/gmail.settings.sharing";


  final commons.ApiRequester _requester;

  UsersResourceApi get users => new UsersResourceApi(_requester);

  GmailApi(http.Client client,
      {core.String rootUrl = "https://www.googleapis.com/",
        core.String servicePath = "gmail/v1/users/"})
      : _requester = new commons.ApiRequester(client, rootUrl, servicePath, USER_AGENT);
}

我找不到使用上述代码的方法,所以我尝试了这个,我认为这是正确的方法,但是没有步骤或文档可以遵循它,这与我在 和 上找到了一些线程和教程DriveApi不同PeopleApi

这仅将值显示为空。我究竟做错了什么?

import 'package:googleapis/gmail/v1.dart';

class ListClass extends StatefulWidget {
  @override
  _ListClassState createState() => _ListClassState();
}

class _ListClassState extends State<ListClass> {

  var leb = Profile();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(leb.emailAddress.toString()),
      ),
    );
  }
}
;

标签: flutterdart

解决方案


推荐阅读