在没有OAuth的情况下将Google API用于自己的帐户 [英] Using Google API's for one's own account without OAuth

查看:78
本文介绍了在没有OAuth的情况下将Google API用于自己的帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我想使用Gmail API访问我自己的邮件 。有没有办法做到这一点,没有OAuth,只是一个API密钥和/或客户端ID和秘密?



使用API​​键:

  require('googleapis')。gmail('v1')。users.messages.list({auth:'< KEY>',userId:' < EMAIL>')}); 

会产生以下错误:

<$ p $
[{domain:'global',
reason:'required',
message:'需要登录',
locationType: 'header',
location:'Authorization'}],
code:401,
message:'需要登录'}

我想这意味着他们想要一个有效的OAuthAuthorization头文件。我会这样做,但我想这是不可能的,如果没有一个网页。

解决方案

严格回答有没有办法在没有OAuth的情况下做到这一点,只需要一个API密钥和/或客户端ID和秘密?是的。



但是,您可以使用OAuth来实现您正在寻找的内容。您只需存储一个Refresh令牌,然后您可以随时使用它来请求Auth令牌访问您的Gmail。



为了获得刷新令牌,您可以写一个简单的网络应用程序来做一次性认证,或者按照这里的步骤如何在没有用户干预的情况下授权应用程序(网页或安装)? (canonical?),它允许您使用Oauth Playground进行整个认证流程。


Specifically, I'd like to use the Gmail API to access my own mail only. Is there a way to do this without OAuth and just an API key and/or client id and secret?

Using an API key like:

require('googleapis').gmail('v1').users.messages.list({ auth: '<KEY>', userId: '<EMAIL>') });

yields the following error:

{ errors: 
   [ { domain: 'global',
       reason: 'required',
       message: 'Login Required',
       locationType: 'header',
       location: 'Authorization' } ],
  code: 401,
  message: 'Login Required' }

I suppose that message means they want a valid OAuth "Authorization" header. I would do that but I suppose that's not possible without presenting a webpage.

解决方案

The strict answer to "Is there a way to do this without OAuth and just an API key and/or client id and secret?" is no.

However, you can achieve what you are looking for using OAuth. You simply need to store a Refresh Token, which you can then use any time to request an Auth Token to access your gmail.

In order to get the refresh token, you can either write a simple web app to do a one time auth, or follow the steps here How do I authorise an app (web or installed) without user intervention? (canonical ?) which allows you to do the whole auth flow using the Oauth Playground.

这篇关于在没有OAuth的情况下将Google API用于自己的帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆