I need to verify the ownership of website using "metatag" in Webmaster Tools - Response 400: Bad Request - verification

I need to verify the ownership of website using "metatag" in Webmaster Tools. I am using Google Webmaster Tool API in Vb.net.
Following are the code logic.
Dim client As New WebClient
Try
Dim query As String
client.Headers.Add("Authorization: GoogleLogin auth=" + _auth)
client.Headers.Add("GData-Version: 2")
client.Headers.Add("Content-Type", "application/atom+xml")
query = "<atom:entry xmlns:atom=""http://www.w3.org/2005/Atom"" " + _
"xmlns:wt=""http://schemas.google.com/webmasters/tools/2007""> " + _
"<atom:id>https://www.google.com/webmasters/tools/feeds/sites/http%3A%2F%2Ftestwebsite2.demos.classicinformatics.com%2F</atom:id> " + _
"<atom:category scheme='http://schemas.google.com/g/2005#kind' " + _
"term='http://schemas.google.com/webmasters/tools/2007#site-info'/> " + _
"<wt:verification-method type=""metatag"" in-use=""true""/> " + _
"</atom:entry>"
Dim response = client.UploadString("https://www.google.com/webmasters/tools/feeds/sites/http%3A%2F%2Ftestwebsite2.demos.classicinformatics.com%2F", _
"PUT", query)
Catch ex As WebException
End Try
In above code, after uploading API string, I am getting 400 Bad Request error, while If successful, the response should be Sites feed with each entry updated and the "verified" element set to "true".

I have been working on Google webmaster api and working with the similar process but stucking with feature to Add site using webmaster tool API.
I am following the process explained in given URL:
https://developers.google.com/oauthplayground/
I followed all three step.
Step 1 : Need to go through the AOuth Login request and get the Authorization code in response.
Step 2 : I am getting the access token in exchange of authorization code.
Step 3 : And this the step I am stuck with.
Here, when I am going to consume this access token for next request, it showing me the unauthorized access (401).
Here is my code to generate the request to add website using webmaster tool api
WebClient client = new WebClient();
var query = "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'><atom:content src=\"" + address + "\" /></atom:entry>";
client.Headers.Add("GData-Version", "2");
client.Headers.Add("Content-Type", "application/atom+xml");
client.Headers.Add("Authorization", "OAuth " + accessToken);
var response = client.UploadString("https://www.google.com/webmasters/tools/feeds/sites/", "POST", query);
//LoadList();

Related

Getting A Refresh Token From Google Using An Authorization Token Posted in Java

I have read many posts, all the Google documentation I can find and tried many iterations of the following and still can't get an access and refresh token. I do get an authorization code but can't seem to get that to trade for the access and refresh tokens.
if(authCode == null || authCode.equals("")) {
String url = "https://accounts.google.com/o/oauth2/v2/auth?"
+ "scope=https://mail.google.com/&"
+ "response_type=code&"
+ "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"
+ "client_id=" + clientId +
"&access_type=offline";
URI uri = new URI(url);
logger.debug("URI for auth is: " + uri);
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(uri);
}
}
else {
logger.debug("Refreshing");
initRefreshToken();
}
With that, I get an access code I can cut and paste (just testing and trying to get this to work first) in my properties to get the refresh and access token.
In the initRefreshToken() method, the source is like this:
if(refreshToken.equals("")) {
logger.debug("Getting refresh token");
HttpPost post = new HttpPost("https://oauth2.googleapis.com/token");
// add request parameter, form parameters
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("code", authCode));
urlParameters.add(new BasicNameValuePair("client_id", clientId));
urlParameters.add(new BasicNameValuePair("client_secret", clientSecret));
urlParameters.add(new BasicNameValuePair("redirect_uri", "http://localhost:8000/"));
urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
System.out.println("***** URL: " + urlParameters);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
}
If this is a second or subsequent time using the code, what will be printed is:
Refersh token:
***** URL: [code=4/1AY0e-g..., client_id=370...i1h2u1s.apps.googleusercontent.com, client_secret=bAOH..., redirect_uri=https://localhost:8000/, grant_type=authorization_code]
{
"error": "invalid_grant",
"error_description": "Bad Request"
}
If the code is run and it's the first time using an authentication code, it will print:
{
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
}
I read in the Google console that exceptions are made for localhost domains so there's no need to register them. However, if there were a need to register them, it won't let you register them anyway as a domain must be a top level domain you own in order to register it. Therefore, how do I register localhost and/or exchange an authorization code for an access and refresh token in Java?
Thank you for your help.
DaImTo provided a great video about this and in that video and the blog post associated with it, the redirect_uri is listed correctly as: "urn:ietf:wg:oauth:2.0:oob". I didn't find this in the documentation but when I added it to my source code, I got access and refresh tokens as a response. Thank you very much for that help, DaImTo.

Updating stock on magento 2.1 using REST API - Error {"message": "Request does not match any route."}

I am trying to get a stock item quantity updated on a magento 2.1 site using REST API.
I am coding in VB.net but I get the error JSON response {"message": "Request does not match any route."}
Dim Access_Token = "XXXXXXXXXXXXX"
Try
Dim VATWebClient = New WebClient()
VATWebClient.Headers(HttpRequestHeader.Accept) = "application/json"
VATWebClient.Headers(HttpRequestHeader.ContentType) = "application/json"
VATWebClient.Headers(HttpRequestHeader.Authorization) = "Authorization Bearer " & Access_Token
Dim Response As String
Response = VATWebClient.UploadString("http://www.xxxxxx.com/rest/V1/products/xxxx/stockItems/1", "{""stockItem"":{""qty"":100}}")
Catch webEx As WebException
Dim errorMessage As String = webEx.Message
Dim errorStack As String = webEx.StackTrace
End Try
I have also tried to setup SoapUI just to test to make sure that I am calling it right and I get the same error.
I read somewhere that the webapi.xml must be updated with the API which is required I am really hoping that's not the case as the host/web developer is not very accessible!
UploadString will create a POST Request, as you can see form the API Docs, this API endpooint is PUT method only.
https://devdocs.magento.com/swagger/index_21.html#!/catalogInventoryStockRegistryV1/catalogInventoryStockRegistryV1UpdateStockItemBySkuPut
I'm not too sure how to change the method in visual basic, but I'm sure it's not too difficult.

How do I authenticate, using Nim's httpclient module to retrieve HTML?

I'm a beginner and I want to write a Nim-application that processes some data from an internal website.
Basic authentication (username, password) is required to access this site.
A working Python solution is:
response = requests.get('https://internal:PORT/page',
auth=('user', 'passwd'),
verify=False) # this is vital
Based on the nim doc regarding httpclient and the modules source code, where it is stated that one could use a proxy as an argument for any of the functions, I've been trying something along these lines:
var
client = newHttpClient()
prox = newProxy("https://internal:PORT/page", "user:passwd")
let response = client.getContent(prox) # Error: type mismatch
The solution is probably very obvious but I'm out of ideas on
how to authenticate.
If anybody could help, that'd be highly appreciated!
Basic auth is just an "Authorization" header with value "Basic " + base64(username + ":" + password). Equivalent in nim:
import httpclient, base64
var
client = newHttpClient()
var username = ...
var password = ...
client.headers["Authorization"] = "Basic " & base64.encode(username & ":" & password)
# ... send request with the client

How to use UrlFetchApp with credentials? Google Scripts

I am trying to use Google Scripts UrlFetchApp to access a website with a basic username and password. As soon as I connect to the site a popup appears that requires authentication. I know the Login and Password, however I do not know how to pass them within the UrlFetchApp.
var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/‎");
Logger.log(response.getContentText("UTF-8"));
Currently running that code returns "Access Denied". The above code does not contain the actual address I am connecting to for security reasons. A "t" is missing from all the "http" in the code examples because they are being detected as links and Stackoverflow does not allow me to submit more than two links.
How can I pass the Login and Password along with my request? Also is there anyway I can continue my session once I have logged in? Or will my next UrlFetchApp request be sent from another Google server requiring me to login again?
The goal here is to login to the website behind Googles network infrastructure so it can act as a proxy then I need to issue another UrlFetchApp request to the same address that would look something like this:
var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/vuze/rpc?json={"method":"torrent-add","arguments":{"filename":"htp://vodo.net/media/torrents/anything.torrent","download-dir":"C:\\temp"}}‎");
Logger.log(response.getContentText("UTF-8"));
This question has been answered on another else where.
Here is the summary:
Bruce Mcpherson
basic authentication looks like this...
var options = {};
options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)};
Lenny Cunningham
//Added Basic Authorization//////////////////////////////////////////////////////////////////////////////////////////
var USERNAME = PropertiesService.getScriptProperties().getProperty('username');
var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');
var url = PropertiesService.getScriptProperties().getProperty('url');//////////////////////////Forwarded
Ports to WebRelay
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
};
var params = {
"method":"GET",
"headers":headers
};
var reponse = UrlFetchApp.fetch(url, params);
I was unable to find user3586062's source links (they may have been deleted), but taking Bruce Mcpherson's approach, your code would look like this:
var options = {};
options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)};
UrlFetchApp.fetch("TARGET URL GOES HERE", options);

No response after authentication success in pentaho?

I try to use Basic Authentication to make a login into Pentaho by using .NET code
My code is:
Dim request = WebRequest.Create("http://x.x.x.x:8080/pentaho/Home")
Dim authInfo As String = Convert.ToString(userName) & ":" & Convert.ToString(userPassword)
authInfo = Convert.ToBase64String(Encoding.[Default].GetBytes(authInfo))
request.Headers("Authorization") = "Basic " & authInfo
Dim response As WebResponse = request.GetResponse()
You can see that I pass the authorization header with Basic username:password with Base64 encode.
for the last line of above code
Dim response As WebResponse = request.GetResponse()
you can see that I get the response from pentaho server then assign to variable response
In running, I found that Pentaho can return response and put the value to variable response.It seem okay that the authentication is passed and I login successfully
but when I input a link http://x.x.x.x:8080/pentaho/Home the login page still prompt and I need to login again....
Do you know what code I missing so that the Pentaho cannot login auto after above code?
Thanks you much!!!!!
Have you tried putting it as parameters in the url i.e :
?userid=joe&password=password
However, looking at it again it seems if you're successfully getting a logged in response, then the next request you are not logged in then you have not preserved the session/cookie between those two requests. Easily done in java with httpclient, not so sure about .net