How to send Token Request?

120 views
Skip to first unread message

Ellen

unread,
Sep 14, 2010, 6:46:08 PM9/14/10
to GlobalGiving API
Hi All,

I am new to GlobalGiving, and using C#.Net; wondering how can I make
this Token Request? I saw the sample call is in cURL program, but how
to plug cURL in C#.net?

Is there someone could point me a direction?

Thank you,

ellen

marcelo.barth

unread,
Sep 14, 2010, 8:01:54 PM9/14/10
to GlobalGiving API
Hi Ellen,

Unfortunately, here at GlobalGiving, we are mainly java developers
with no C# experts on staff. Searching for some sample code, I found a
C# snippet I modified that may help. Warning, it's untested.

Hope it helps.

Marcelo

####################################################

WebRequest req = null;
WebResponse rsp = null;
try {
string email = "REPLACE_WITH_YOUR_EMAIL"
string password = "REPLACE_WITH_YOUR_PASSWORD"
string api_key = "REPLACE_WITH_YOUR_API_KEY"
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?
><auth_request><user><email>" + email + "</email><password>" +
password + "</password></user><api_key>" + api_key + "</api_key></
auth_request>";
string uri = "https://api-test.globalgiving.org/api/userservice/
tokens";
req = WebRequest.Create(uri);
req.Method = "POST"; // Post method
req.ContentType = "text/xml"; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(xml);
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
}
catch (WebException webEx) {

}
catch (Exception ex) {

}
finally {
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}

####################################################

Ellen

unread,
Sep 15, 2010, 9:23:02 AM9/15/10
to GlobalGiving API
Thank you very much for your quick response, Marcelo!

It really helps.

Ellen

Ellen

unread,
Sep 16, 2010, 12:07:36 PM9/16/10
to GlobalGiving API
Hi Marcelo,

I used code below, but I had the error says "The remote server
returned an error: (400) Bad Request." It means the call reached the
server, but since the request does not succeed, Do you think if there
are some security reasons prevent me from accessing or my credential
didn't match? Thank you in advance for your help.

WebRequest req = null;
WebResponse rsp = null;
try {

string xml = "<?xml version=\"1.0\" encoding=
\"UTF-8\"? ><auth_request><user><email>j...@yourcause.com</
email><password>8005case</password></user><api_key>aada3015-2f84-4eab-
bf00-4d5315fe2b05</api_key></ auth_request>";
string uri = "https://api-test.globalgiving.org/api/
userservice/tokens";
req = WebRequest.Create(uri);
req.Method = "POST"; // Post method
req.ContentType = "text/xml";

byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(xml);

req.ContentLength = bytes.Length;

Stream outputStream = req.GetRequestStream();
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Close();
rsp = req.GetResponse();
Stream MyStream = rsp.GetResponseStream();
StreamReader myStreamReader = new
StreamReader(MyStream);
myStreamReader.ReadToEnd().Trim();

}

catch (WebException ex) {
throw new WebException("Failed to get the request
stream.", ex);

}
catch (Exception e) {
throw new Exception("Failed to get the request
stream.", e);

}
finally {
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}


Ellen

marcelo.barth

unread,
Sep 16, 2010, 1:05:34 PM9/16/10
to GlobalGiving API
Hi Ellen,

The accounts on our test server (api-test) were not fully replicated
with the accounts from production. It has been updated so you should
be able to retrieve a token now.

I ran a test using the credentials you provided and retrieved a token
successfully pointing to our test server.

Let us know if you experience anymore issues.

Thanks,
Marcelo

Ellen

unread,
Sep 16, 2010, 3:34:15 PM9/16/10
to GlobalGiving API
Marcelo,

The code will stop at rsp = req.GetResponse();, and I am getting the
same error-- bad request.

have you counter this kind of issue before?

what I could think is 1)request didn't well formed? 2) request didn't
match your end format? 3)or simply end won't pick a call from .net
platform?

Sorry to bother you again,

ellen

marcelo.barth

unread,
Sep 16, 2010, 4:13:45 PM9/16/10
to GlobalGiving API
Hi Ellen,

I just improved the error message to highlight the error that is
occurring. In your case there were 2 problems with the XML submitted.

1) the xml declaration has an extra space in it at the end after the
'?': <?xml version=\"1.0\" encoding=\"UTF-8\"? > should be <?xml
version=\"1.0\" encoding=\"UTF-8\"?>

2) there's an extra space after the closing '/' in the auth_request
element: </ auth_request> should be </auth_request>

Now the error messages will look like the following, highlighting the
problem:

1)

<?xml version='1.0' encoding='UTF-8'?>
<error_response>
<error_code>400</error_code>
<errors>
<error>
<error_message>Problem with received message of content type
[application/xml]. Problem area [The XML declaration must end with "?
>".]</error_message>
</error>
</errors>
<status>Bad Request</status>
</error_response>

and

2)

<?xml version='1.0' encoding='UTF-8'?>
<error_response>
<error_code>400</error_code>
<errors>
<error>
<error_message>Problem with received message of content type
[application/xml]. Problem area [The element type "auth_request" must
be terminated by the matching end-tag "&lt;/auth_request>".]</
error_message>
</error>
</errors>
<status>Bad Request</status>
</error_response>

Hope that helps in your development.

Thanks,
Marcelo

Ellen

unread,
Sep 16, 2010, 6:55:00 PM9/16/10
to GlobalGiving API
Marcelo,

Now I get token!

Well, I had same "bad request" when I try my test donation.


Here is the donation string which I copied from your site:
string xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?
>donation><refcode>123456789</refcode><transactionId>abc123</
transactionId><email>john...@hotmail.com</email><amount>25</
amount><project><id>1883</id></
project><payment_detail><firstname>John</firstname><lastname>Doe</
lastname><address>3380 Pearlman Avenue</address><address2>Suite 200</
address2><city>Washington</city><state>DC</
state><iso3166CountryCode>US</iso3166CountryCode><zip>20001</zip></
donation>";

Path is https://api-test.globalgiving.org/api/secure/givingservice/donation&api_key=MyKey&api_token=MyToken&is_test=true

One thing I am not sure is the refcode, in your site says" your
external identifier for this request which is returned in the
response, required (maximum 100 characters)". I am sending a donation
request, and waiting for response; then what value should I put in?

Thank you,
ellen

marcelo.barth

unread,
Sep 16, 2010, 7:34:31 PM9/16/10
to GlobalGiving API
Hi Ellen,

When copying and pasting our example XML, it looks like you deleted
one too many characters when getting rid of the carriage returns.

The donation element is not properly formatted, therefore, our parser
could not unmarshall your request.

donation> should be <donation>

The refcode element is there to allow you to track the request through
our system.

When you receive a response the refcode returned should match the one
you provided in your request.

The value to provide for refcode, is any unique identifier you want to
use, limited to 100 characters that is.

Marcelo


On Sep 16, 3:55 pm, Ellen <ellen.hu...@gmail.com> wrote:
> Marcelo,
>
> Now I get token!
>
> Well,  I had same "bad request" when I try my test donation.
>
> Here is the donation string which I copied from your site:
>    string xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>donation><refcode>123456789</refcode><transactionId>abc123</
>
> transactionId><email>john....@hotmail.com</email><amount>25</
> amount><project><id>1883</id></
> project><payment_detail><firstname>John</firstname><lastname>Doe</
> lastname><address>3380 Pearlman Avenue</address><address2>Suite 200</
> address2><city>Washington</city><state>DC</
> state><iso3166CountryCode>US</iso3166CountryCode><zip>20001</zip></
> donation>";
>
> Path ishttps://api-test.globalgiving.org/api/secure/givingservice/donation&a...

Ellen

unread,
Sep 17, 2010, 10:32:10 AM9/17/10
to GlobalGiving API
Marcelo,

Oops I need pay attention.

Now my request looks ok, but I received an error says "You do not have
the proper permission to execute this operation." during the test
donation access.

I saw the token lifetime by default is set to 10 hours. So first I get
the token value, then when I click the "Submit" button, send out
donation request.

here is the calling url---
https://api-test.globalgiving.org/api/secure/givingservice/donations?api_key=aada3015-2f84-4eab-bf00-4d5315fe2b05&api_token=gh31l258z08u3r4h58z1vid2h858z2c4y5rd58z3&is_test=true

Am I doing something wrong?

Thank you,

ellen

Ellen

unread,
Sep 17, 2010, 12:53:35 PM9/17/10
to GlobalGiving API
Marcelo,

Please ignore my question, it has been completed.

Thank you for your help.

Jing

On Sep 17, 9:32 am, Ellen <ellen.hu...@gmail.com> wrote:
> Marcelo,
>
> Oops I need pay attention.
>
> Now my request looks ok, but I received an error says "You do not have
> the proper permission to execute this operation." during the test
> donation access.
>
> I saw the token lifetime by default is set to 10 hours. So first I get
> the token value, then when I click the "Submit" button, send out
> donation request.
>
> here is the calling url---https://api-test.globalgiving.org/api/secure/givingservice/donations?...
Reply all
Reply to author
Forward
0 new messages