谷歌oauth2获取令牌javascript发布请求 [英] google oauth2 get token javascript post request

查看:148
本文介绍了谷歌oauth2获取令牌javascript发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些关于这个问题的答案,但不知道该怎么做。
我得到这个错误: XMLHttpRequest无法加载https://accounts.google.com/o/oauth2/token。请求的资源上没有Access-Control-Allow-Origin标题。原因'http:// localhost:63342'因此不被允许访问。这个响应的HTTP状态码是400.
正如我从以前的帖子看到的,这是因为我无法向另一台服务器发送HTTP POST请求。我看到了一些关于使用jsonp但是无法理解的方法。
这是我用来发送请求的函数:

  var url ='https://accounts.google.com/o/oauth2/token'; 
var payload = {
grant_type:'authorization_code',
code:authResult ['code'],
client_id:clientID,
client_secret:clientSecret,
redirect_uri:'',
dataType:'jsonp'
};
$ b $ .post(url,{
form:payload
},function(error,response,body){
console.log(body);
});


解决方案

您是否注册了您的应用程序?


获取OAuth密钥:




  • 访问Google Cloud Console

  • 单击CREATE PROJECT按钮

  • 输入项目名称,然后单击CREATE

  • 然后选择APIs&点击创建新的客户端ID按钮

  • 应用程序类型:Web应用程序 / li>
  • 授权的Javascript出处: http:// localhost:63342

  • 授权重定向网址: http:// localhost:63342 / ...



注意:确保已打开所需的API。
$ b 重要的部分是:授权的Javascript起源: http:// localhost:63342 ,您需要自行确定您的网站域以访问API。

b
$ b

端点不好使用 https://www.googleapis.com/oauth2/v3/token Google Doc

  $。ajax({
url:https://www.googleapis.com/ oauth2 / v3 / token,
data:{
code:,
client_id:,
client_secret:,
redirect_uri:,
grant_type:authorization_code
},
方法:POST,
success:function(e){console.log(e)}
});


I saw some questions and answers about this but couldn't understand what to do. I get this error: XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 400. As I saw from previous posts, this is because I can't make an HTTP POST request to another server. I saw some things about using jsonp but couldn't understand how.. This is the function I use to send the request:

var url = 'https://accounts.google.com/o/oauth2/token';
var payload = {
    grant_type: 'authorization_code',
    code: authResult['code'],
    client_id: clientID,
    client_secret: clientSecret,
    redirect_uri: '',
    dataType: 'jsonp'
};

$.post(url, {
    form: payload
}, function(error, response, body) {
    console.log(body);
});

解决方案

Have you registered your app ?

Obtaining OAuth Keys :

  • Visit Google Cloud Console
  • Click CREATE PROJECT button
  • Enter Project Name, then click CREATE
  • Then select APIs & auth from the sidebar and click on Credentials tab

Note: Make sure you have turned on the APIs you need.

The important part is : Authorized Javascript origins: http://localhost:63342, you need to autorize your website domain to access the API.

The endpoint is bad use https://www.googleapis.com/oauth2/v3/token Google Doc:

$.ajax({
    url: "https://www.googleapis.com/oauth2/v3/token",
    data: {
        code :"",
        client_id : "",
        client_secret : "",
        redirect_uri : "",
        grant_type : "authorization_code"
    },
    method: "POST",
    success: function(e){console.log(e)}
});

这篇关于谷歌oauth2获取令牌javascript发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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