B4J Question Notification UnAuthorized

derez

Expert
Licensed User
Longtime User
I have this code
B4X:
...
json.Initialize(m)
Dim Job As HttpJob
Job.Initialize("send message", Me)
Job.PostString("https://android.googleapis.com/gcm/send", json.ToString)
Job.GetRequest.SetContentType("application/json")
Job.GetRequest.SetHeader("Authorization", "key=" & Main.config.Get("AndroidApiKey"))
End Sub
It fails with this message
B4J version: 4.20 (1)
Parsing code. (0.02s)
Compiling code. Error
Error compiling program.
Error description: Too many parameters.
Occurred on line: 39
Job.PostString("https://android.googleapis.com/gcm/send", json.ToString)
Word: json
The used libraries are:
upload_2016-4-28_23-52-46.png

I don't understand the error - there are two parameters.
 

LucaMs

Expert
Licensed User
Longtime User
I could be wrong (I've never used Json... directly) but you have created a variable with the same name of a library (json); this should be the error.

Usually I use an "m" as prefix (for other reasons): try to rename it to mJson (or, better, mJSONGenerator, JSONGenerator is its type, I suppose)
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Thanks for looking into it !.
The problem is not this. It is doing notifications and I should do it step by step, not try to make shortcuts...
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
that is strange, I can run your example without any compiling error. json is a JSONGenerator Type?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Like I said above - the problem relates to the notification process. I have gone back to the example for notification (GCM). Now I have this running but I get another problem - the notification is unauthorized:
Waiting for debugger to connect...
Program started.
2016-04-29 12:24:32.766:INFO::main: Logging initialized @815ms
2016-04-29 12:24:32.853:INFO:eek:ejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-04-29 12:24:32.898:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@7c75222b{/,file:///C:/Users/dudu/Downloads/PushServer/Objects/www,AVAILABLE}
2016-04-29 12:24:32.901:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\Users\dudu\Downloads\PushServer\Objects\logs\b4j-2016_04_29.request.log
2016-04-29 12:24:32.966:INFO:eek:ejs.ServerConnector:main: Started ServerConnector@7f560810{HTTP/1.1,[http/1.1]}{0.0.0.0:54025}
2016-04-29 12:24:32.967:INFO:eek:ejs.Server:main: Started @1017ms
Emulated network latency: 100ms
server version: 0.97
server is listening on port: 54025
Table: tokens
Token added: APA91bEmjSv1rFm8r7Hn7VNhCUR74V-geWumS0UjHhWHWiaa9guIKNU4C08K_GDu0OTwT9rWTV8cgYbRGzhUXUsQTRsYVPJnBIPEuVRellijnuyKhaatpYed59oQs29pWxAHksplNzQ1
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Error sending Android message: Unauthorized
I have done the required actions in google gcm and put the apikey in the config file and the project number in the b4a application. ???
The error happens when this line in AndroidPush module is run, the key is correct:
B4X:
Job.GetRequest.SetHeader("Authorization", "key=" & Main.config.Get("AndroidApiKey"))
In the b4a app I get this log but nothing is received:
...
Token uploaded successfully.
sent
success
Message sent to 1 device(s).

When changing the key to a key that I have in another application the response is that the message was sent successfully, but the notification has not been received.
 
Last edited:
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Hi, I'm with the same problem.. since yesterday night
I was looking for some help into the forum and saw that "Unauthorized" is related with the Api Key from the google dev console
But, I did not change nothing on the key.. and also generate a new key to be sure about that.. same error
The Api is enabled into google dev console (see the image attached)
Do somebody knows if some terms to use GCM Push have changed?
I can not figure it out what is wrong
Thanks for any help
 

Attachments

  • CGM Push.png
    CGM Push.png
    7 KB · Views: 191
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Hi Erel, it was something weird.. but, fortunately, I already found a solution!

Reading this tutorial:
https://www.b4x.com/android/forum/t...otification-gcm-framework-and-tutorial.19226/
I noticed that you've mentioned about the Api Key as a "Browser Key" and I've defined it as an "Android Key"

It's a bit confused that, for some reason, this was working fine since 2014-01-01 until yesterday night

I'm thinking that this could be the problem because If I switch it again to the "Android Key" the "Unauthorized" error is raised (both the old one and the new one generated today)
And backing up it again to the "Browser Key" does work perfect

Thanks so much!
 
Upvote 0

Richard Goh

Active Member
Licensed User
Longtime User
Hi,
I am having the same problem sending message in this week. I managed to run the service successfully before that. But this few days, I got the unauthorized message console Logs. I had recreate the API key and set on the config. Screen show successfully send. But B4J log shown unauthorized and devices doesn't receive any message. Any advice on this?
Thanks in advance.
 
Upvote 0

Richard Goh

Active Member
Licensed User
Longtime User
Hi woniol,
Thank you very much for the reply. It's solved the problem and saved my time. I has been trying for last 2 days troubleshooting on the problem and looking on b4x forum as well. Thanks again.
 
Last edited:
Upvote 0
Top