I need help in getting the google contact details username, email, picture. This code is working fine fetching email:
var config = {
'client_id': 'XXXXXXXXXXXX.apps.googleusercontent.com',
//'scope': 'https://www.googleapis.com/auth/urlshortener',
'scope': 'https://www.google.com/m8/feeds'
};
gapi.auth.authorize(config, function () {
var authParams = gapi.auth.getToken(); // from Google oAuth
authParams.alt = 'json';
$.ajax({
url: 'https://www.google.com/m8/feeds/contacts/default/full',
dataType: 'jsonp',
data: authParams,
success: function (data) {
alert(JSON.stringify(data))
}
});
});
This data display only user email address but I also want username and profile pic.
Is this posssible? Please help me.
Related
I need to access datas from Search Console using Apps Script.
I tried a loads of things but nothing worked.
I'm using this doc : https://developers.google.com/webmaster-tools/v1/searchanalytics/query
Here are some things I tried :
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/webmasters https://www.googleapis.com/auth/webmasters.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); })}
function loadClient() {
gapi.client.setApiKey("YOUR_API_KEY");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/searchconsole/v1/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); })}
function execute() {
return gapi.client.webmasters.searchanalytics.query({
"resource": {}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); })}
Other try:
function searchConsoleQuery() {
var service = getService();
var apiURL =
'https://www.googleapis.com/webmasters/v3/sites/[SITE_URL]/searchAnalytics/query';
var headers = {
'Authorization': 'Bearer ' + service.getAccessToken(),
'contentType':'application/json',
'startDate':'20019-10-01',
'endDate':'2019-10-10'};
var options = {
'payload': JSON.stringify(headers),
'method' : 'POST',
'muteHttpExceptions': true};
var response = UrlFetchApp.fetch(apiURL, options);
var json = JSON.parse(response.getContentText());
Logger.log(json)}
Modification points:
In your script, headers is used to payload.
'startDate' is 20019-10-01.
When these points are reflected in your script, it becomes as follows.
Modified script:
function searchConsoleQuery() {
var siteUrl = "###"; // Please set your site URL.
var service = getService();
var apiURL = `https://www.googleapis.com/webmasters/v3/sites/${encodeURIComponent(siteUrl)}/searchAnalytics/query`;
var options = {
'method': 'POST',
'muteHttpExceptions': true,
'headers': { 'Authorization': 'Bearer ' + service.getAccessToken() },
'contentType': 'application/json',
'payload': JSON.stringify({
'startDate': '2019-10-01',
'endDate': '2019-10-10'
})
};
var response = UrlFetchApp.fetch(apiURL, options);
var json = JSON.parse(response.getContentText());
Logger.log(json)
}
Note:
In this modification, it supposes that Google Search Console API has already been enabled at API console and also siteUrl and your access token of service.getAccessToken() are the valid values for using the API. Please be careful about this. When I tested this modified script with my site URL, I confirmed that the values are returned without error. So, if an error occurs, please confirm the condition of API in your API console and your values again.
If no values are returned, please modify 'startDate': '2019-10-01' and 'endDate': '2019-10-10' and test it again.
References:
Search Analytics: query
fetch(url, params)
I need to request information from api.I tried to make a request with UrlFetchApp.fetch and fetchAll.In both cases i got nothing.Here s my code:
var request1 = {
url: "https://seo-fast-audit.p.rapidapi.com/?url=" + url,
method : 'GET',
params: {url: 'https://docteurseo.fr/'},
headers: {
"x-rapidapi-host": "seo-fast-audit.p.rapidapi.com",
"x-rapidapi-key": "KEY"
}
};
let response = UrlFetchApp.fetchAll([request1])
(here i replaced key)
So what is my problem?Is that problem in async functions or am i requesting not correctly?
Here s API i am using
https://rapidapi.com/DocteurSEO/api/seo-fast-audit
If you want to convert the following javascript to Google Apps Script, Ref
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://seo-fast-audit.p.rapidapi.com/',
params: {url: 'https://docteurseo.fr/'},
headers: {
'x-rapidapi-host': 'seo-fast-audit.p.rapidapi.com',
'x-rapidapi-key': 'SIGN-UP-FOR-KEY'
}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
how about the following modification?
function myFunction() {
var url = "https://seo-fast-audit.p.rapidapi.com?url=" + encodeURIComponent('https://docteurseo.fr/');
var option = {
headers: {
"x-rapidapi-host": "seo-fast-audit.p.rapidapi.com",
"x-rapidapi-key": "KEY"
}
};
let response = UrlFetchApp.fetch(url, option);
console.log(response.getContentText())
}
In your script, params is not included in the object for fetch and fetchAll. And, I thought that in your situation, url is required to do the URL encode, and com/?url= is com?url=.
Note:
I think that the request of the above Google Apps Script is the same as the top of Javascript. But if an error occurs, please check your KEY again.
If an error of 403 forbidden occurs, the site might not be accessed from the Google side. I'm worried about this.
Reference:
fetch(url, params)
so I have been testing a bunch of new different JS code to see if I can get the POST request to send information to a SharePoint list. When I run my newest block, it tells me "Error: Line 20: Unexpected token" when I call $.ajax saying the period is what is unexpected? I truly don't understand.
Here is my JS code:
$(document).ready(function(){
$("#btnSubmit").click(function(){
saveDelivDetails();
});
});
function saveDelivDetails(){
var itemType = GetListItemTpye(listname);
var item = {
"__metadata":{"type":itemType},
"Program":$("#dProgram").val(),
"Deliverable":$("#dDeliverable").val(),
"To":$("#dTo").val(),
"Date":$("#dDate").val(),
"Approved":$("#dApproved").val(),
"Notes":$("#dNotes").val()
};
var requestUrl = _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('"+listname+"')/items",
$.ajax({
url: requestUrl,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers:{
"Accept":"application/json;odata=verbose",
"X-RequestDigest":$("#__REQUESTDIGEST").val()
},
success: onSuccess,
error: onError,
});
function onSuccess(data){
alert("New Item Created");
$("#txtSubmitName").val();
}
function onError(error){
alert('error' + error);
console.log(error);
}
function GetListItemType(name){
return "SP.Data."+CharacterData(0).toUpperCase().name.slice(1)+"ListItem";
}
You have a , after line 19.
var requestUrl = _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('"+listname+"')/items",
I need validate user's nickname when the gonna registre in my proyect:
My Controller
$inputData = Input::get('nickname');
parse_str($inputData);
$informacion = array('nickname' => $inputData);
$regla = array('nickname'=>'required|unique:usuarios|alpha_num|min:6|max:15');
if($request->ajax()){
$usuarios = Usuarios::Usuarios();
$validar = Validator::make($informacion,$regla);
if($validar->fails()){
return Response()->json(array(
'fail' => true,
'errors' => $validar->getMessageBag()->toArray()));
}
else{
return Response()->json(array('success' => true, "message" => "Nombre de Usuario Disponible"));
}
}
My Script
$( "#validar" ).click(function( event ) {
var dato = $('#nickname').val();
var route = $('#form-sign-up').attr('action');
var tipo = $('#form-sign-up').attr('method');
var token = $('#form-sign-up #token').val();
$.ajax({
url: route,
headers: {'X-CSRF-TOKEN': token},
type: tipo,
dataType: 'json',
data:{nickname: dato},})
.fail(function(data){
$('#nickname-msj').html(data.errors);
$('#nickname-msj').fadeIn();
})
.done(function(data) {
$('#nickname-msj').html(data.message);
$('#nickname-msj').fadeIn();
});
});
.done works, but .fails not, and i need display that information to my user, because so they can know what is the problem, if someone can help me will be great.
I am using Laravel 5.2
Thank you.
The fails function of Ajax is triggered with a code different from 200. So you can return
if($validar->fails()) {
return response()->json($arrayHere, 400); //HTTP 400 error for bad request
}
So, just basically add 400 after your array, inside of json() function. When you don't specify the code, it defaults to 200.
Maybe also change your ajax request ? Or not
$.ajax({
url: route,
headers: {'X-CSRF-TOKEN': token},
type: tipo,
dataType: 'json',
data:{
nickname: dato
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log('Error:', data);
}
});
I need to convert this OData retrieval (using SAPUI5 libs):
var url = "http://someurl/SERVICE";
var username = "username";
var password = "password";
var oModel = new sap.ui.model.odata.ODataModel(url, true, username, password);
into an ajax call, without using SAPUI5 libraries, mantaining the authentication:
$.ajax({
url : "http://someurl/SERVICE?$format=json",
type: "GET", //or POST?
dataType: "json",
data : {username : "username", password: "password"},
success: function(){alert("ok")},
error: function(){alert("error")}
})
I googled a lot but I didn't find anything useful.. I don't know how can I do the authentication.
is it possible? Any idea?
Instead of setting "data:..."
you need to handle this in such way :
$.ajax({
url : "http://someurl/SERVICE?$format=json",
type: "GET", //or POST?
dataType: "jsonp",
xhrFields:
{
withCredentials: true
},
beforeSend: function (request)
{
request.setRequestHeader("Authorization", "Basic dG9tY2F0OnRvbWNhdA==");
},
success: function(){alert("ok")},
error: function(){alert("error")}
})
Where "dG9tY2F0OnRvbWNhdA==" is base64 encoded user and password.
And of course, this will work only for Basic authentication.
Or another option is to put user/password in the url directly :
$.ajax({
url : "http://user:password#someurl/SERVICE?$format=json",
type: "GET", //or POST?
dataType: "jsonp",
success: function(){alert("ok")},
error: function(){alert("error")}
})