Javascript 他们是否有办法通过api从谷歌广告账户获取指标?

Javascript 他们是否有办法通过api从谷歌广告账户获取指标?,javascript,html,httprequest,api-design,Javascript,Html,Httprequest,Api Design,我试图通过Oauth提示符显示谷歌广告账户中的指标,这些账户给了我许可。有没有显示通过同意提示的谷歌广告账户数据的方法?如果可能,请修复下面列出的代码,以便我可以通过getElementById在表上显示数据(如果可能) 我试过的代码 <html> <head> </head> <body> <script> var YOUR_CLIENT_ID = '504111780457-nslu2gim01j3c7optfbvjkd7trdg

我试图通过Oauth提示符显示谷歌广告账户中的指标,这些账户给了我许可。有没有显示通过同意提示的谷歌广告账户数据的方法?如果可能,请修复下面列出的代码,以便我可以通过getElementById在表上显示数据(如果可能)

我试过的代码

<html>
<head>

</head>
<body>
<script>
var YOUR_CLIENT_ID = '504111780457-nslu2gim01j3c7optfbvjkd7trdgjgk9.apps.googleusercontent.com';
var YOUR_REDIRECT_URI = 'https://prompt.sewellstephens.com/api';
var fragmentString = location.hash.substring(1);

// Parse query string to see if page request is coming from OAuth 2.0 server.
var params = {};
var regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(fragmentString)) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
if (Object.keys(params).length > 0) {
localStorage.setItem('oauth2-test-params', JSON.stringify(params) );
if (params['state'] && params['state'] == 'try_sample_request') {
  trySampleRequest();
}
}

// If there's an access token, try an API request.
// Otherwise, start OAuth 2.0 flow.
function trySampleRequest() {
var params = JSON.parse(localStorage.getItem('oauth2-test-params'));
if (params && params['access_token']) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET',
      'https://googleads.googleapis.com/v3/customers/1234567890/metrics:mutate?fields=user&' +
      'access_token=' + params['access_token']);
  xhr.onreadystatechange = function (e) {
    if (xhr.readyState === 4 && xhr.status === 200) {
      console.log(xhr.response);
    } else if (xhr.readyState === 4 && xhr.status === 401) {
      // Token invalid, so prompt for user permission.
      oauth2SignIn();
    }
  };
  xhr.send(null);
} else {
  oauth2SignIn();
}
}

/*
 * Create form to request access token from Google's OAuth 2.0 server.
 */
function oauth2SignIn() {
// Google's OAuth 2.0 endpoint for requesting an access token
var oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';

// Create element to open OAuth 2.0 endpoint in new window.
var form = document.createElement('form');
form.setAttribute('method', 'GET'); // Send as a GET request.
form.setAttribute('action', oauth2Endpoint);

// Parameters to pass to OAuth 2.0 endpoint.
var params = {'client_id': YOUR_CLIENT_ID,
              'redirect_uri': YOUR_REDIRECT_URI,
              'scope': 'https://www.googleapis.com/auth/adwords',
              'state': 'try_sample_request',
              'include_granted_scopes': 'true',
              'response_type': 'token'};

// Add form parameters as hidden input values.
for (var p in params) {
  var input = document.createElement('input');
  input.setAttribute('type', 'hidden');
  input.setAttribute('name', p);
  input.setAttribute('value', params[p]);
  form.appendChild(input);
}

// Add form to page and submit it to open the OAuth 2.0 endpoint.
document.body.appendChild(form);
form.submit();
}
</script>

 <button onclick="trySampleRequest();">Prompt Consent</button>
</body>
</html>'''

var YOUR_CLIENT_ID='504111780457-nslu2gim01j3c7optfbvjkd7trdgjgk9.apps.googleusercontent.com';
var您的\u重定向\u URI='0https://prompt.sewellstephens.com/api';
var fragmentString=location.hash.substring(1);
//分析查询字符串以查看页面请求是否来自OAuth 2.0服务器。
var params={};
var regex=/([^&=]+)=([^&]*)/g,m;
while(m=regex.exec(fragmentString)){
参数[decodeURIComponent(m[1])]=decodeURIComponent(m[2]);
}
if(Object.keys(params.length>0){
setItem('oauth2-test-params',JSON.stringify(params));
if(params['state']&¶ms['state']=='try\u sample\u request'){
trySampleRequest();
}
}
//如果有访问令牌,请尝试API请求。
//否则,启动OAuth 2.0流。
函数trySampleRequest(){
var params=JSON.parse(localStorage.getItem('oauth2-test-params');
if(params&¶ms['access\u token']){
var xhr=new XMLHttpRequest();
xhr.open('GET',
'https://googleads.googleapis.com/v3/customers/1234567890/metrics:mutate?fields=user&' +
“访问令牌=”+参数[“访问令牌]);
xhr.onreadystatechange=函数(e){
如果(xhr.readyState==4&&xhr.status==200){
console.log(xhr.response);
}else if(xhr.readyState==4&&xhr.status==401){
//令牌无效,因此提示输入用户权限。
oauth2SignIn();
}
};
xhr.send(空);
}否则{
oauth2SignIn();
}
}
/*
*创建表单以从Google的OAuth 2.0服务器请求访问令牌。
*/
函数oauth2SignIn(){
//用于请求访问令牌的Google OAuth 2.0端点
var oauth2Endpoint=https://accounts.google.com/o/oauth2/v2/auth';
//创建元素以在新窗口中打开OAuth 2.0端点。
var form=document.createElement('form');
form.setAttribute('method','GET');//作为GET请求发送。
form.setAttribute('action',oauth2Endpoint);
//要传递给OAuth 2.0端点的参数。
var params={'client\u id':您的\u client\u id,
“重定向\u uri”:您的\u重定向\u uri,
'范围':'https://www.googleapis.com/auth/adwords',
'state':'try_sample_request',
“包含授予的范围”:“true”,
'响应类型':'令牌'};
//添加表单参数作为隐藏的输入值。
for(参数中的var p){
var input=document.createElement('input');
setAttribute('type','hidden');
input.setAttribute('name',p);
input.setAttribute('value',params[p]);
表单。追加子项(输入);
}
//将表单添加到页面并提交以打开OAuth 2.0端点。
文件.正文.附件(表格);
表单提交();
}
立即同意
'''
我不知道把开发者令牌放在哪里,如果可能的话,需要有人来修复这段代码。我一直在尝试做这个api调用,试图修复这段代码,以便通过同意提示显示来自google ads帐户的数据,这是一场噩梦