我正在开发一个flutter应用程序,它有一个模块可以帮助用户收听他的gmail并通知他是否有新邮件发送,它看起来像telegram
上的这个机器人例如,我想列出他的所有消息,所以我去了这个 api 并在 postmant 上尝试它
我作为他们的导游的要求是
curl --location --request GET 'https://gmail.googleapis.com/gmail/v1/users/me/profile?key=AIzaSyB4Eg2A34XlMp89XEdLA0aJUFAsTPn9B3o' \
--header 'Authorization: Bearer ya29.a0Ad52N395ZScRVeFUSJvAaD8jIQQE3BpdZf4awh-W_XmNc3Gb92N2CPw5Sc3BYz6QrOfPwuqP1YkzMoBxuuYAzkV7vy4MJ2alLiNI3uOfyET9Yiwga7vpVkc81LY5_4FD-ejw83DyOV9janee8EFnKo6JIafbzS6_MeFVXxfTawaCgYKAa4SARESFQHGX2Mi_H5hxg6MtI_NEFJXp91Ngw0177' \
--header 'Content-Type: application/json'
但是我得到了这个回复
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Invalid Credentials",
"domain": "global",
"reason": "authError",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"
}
}
我尝试使用 flutter 进行 Oauth
oauth2:^2.0.2 oauth2_client:^3.2.2 flutter_appauth:^6.0.4 googleapis_auth:^1.5.0 Extension_google_sign_in_as_googleapis_auth:^2.0.12
没有人为我工作,总是缺少一些东西,比如
customUriScheme / redirectUri /discoveryUrl / etc
任何人都可以帮助我如何处理gmail api吗?
我没有在 Postman 中进行测试,而是选择直接在应用程序中进行编码。利用 google_sign_in 包,我获取了当前用户的访问凭据/标头,并利用它们通过 Google 的 API 包(googleapis)进行身份验证。
使用 google_sign_in 获取 AuthHeaders
await _googleSignIn.signIn();
Map<String, String>? headers = await _googleSignIn.currentUser?.authHeaders;
final authHeaders = widget.headers; // usersAuthHeaders
final authenticateClient = GoogleAuthClient(authHeaders);
gmailApi = gMail.GmailApi(authenticateClient);
按照此设置,您可以继续迭代并检索消息:
for (gMail.Message message in results.messages ?? []) {
// Your code here
}