0

I am working on a program which share data to LinkedIn User’s Profile. I am using Delphi XE2, OAuth and LinkedIn API for the same. I am able to get Access token. Then I want to update status. So my code is

procedure TForm1.Button2Click(Sender: TObject);
var
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL ;
Url,sign  : String;
Response     : TStringStream;
slist        : TStringList;
str          : WideString;
Arequest1    : TOAuthRequest;
AuthHeader   : WideString;
begin
   with http do
   Begin
    IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSOcketOpenSSL.Create(nil);
    with idSSLIOHandlerSocketOpenSSL1 do
    begin
      SSLOptions.Method := sslvTLSv1;
      SSLOptions.SSLVersions := [sslvTLSv1];
      SSLOptions.Mode := sslmBoth;
      SSLOptions.VerifyMode := [];
      SSLOptions.VerifyDepth := 0;
      host := '';
    end;
    IOHandler := IdSSLIOHandlerSocketOpenSSL1;
    AllowCookies := True;
    Request.ContentRangeEnd := 0;
    Request.ContentRangeStart := 0;
    Request.ContentType := 'application/xml';
    Request.ContentEncoding := 'utf-8';
    Request.BasicAuthentication := False;
    Request.Connection   := 'Keep-Alive';
    request.host := 'api.linkedin.com';
    Request.Accept := 'text/xml, */*';
    Request.UserAgent  :=  'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0' ;
    HTTPOptions := [hoForceEncodeParams];
   end;
     Url := 'https://api.linkedin.com/v1/people/~/shares';
     Consumer    := nil;
     Consumer    := TOAuthConsumer.Create(ConsumerKey, ConsumerSecret);
     Arequest1   := TOAuthRequest.Create(Url);
     sign        := HMAC.build_signature(Arequest1,Consumer,Token);
     sign        := TOAuthUtil.urlEncodeRFC3986(sign);
     http.Request.CustomHeaders.Clear;
     AuthHeader     := 'OAuth '+ 'oauth_nonce="'+Arequest1.GenerateNonce+'",'+  'oauth_signature_method="'+HMAC.get_name+'",'+ 'oauth_timestamp="'+Timestamp+'",'+ 'oauth_consumer_key="'+Consumer.Key+'",'+ 'oauth_token="'+Token.Key+'",'+ 'oauth_signature="'+sign+'",'+ 'oauth_version="1.0"';
      http.Request.CustomHeaders.Add(AuthHeader);
      slist       := TStringList.Create;

      slist.Text  := '<?xml version="1.0" encoding="UTF-8"?><share><comment>Posting from the API using XML</comment>'+
                     '<content><title>A title for your share</title><submitted-url>http://developer.linkedin.com</submitted-url>'+
                     '<submitted-image-url>http://lnkd.in/Vjc5ec</submitted-image-url></content><visibility>'+
                     '<code>anyone</code></visibility></share>';
    Response    := TStringStream.Create;
    http.Request.ContentLength  := length(slist.Text);
  try
     http.post(Url,slist,Response);
  Finally
    ShowMessage(Response.DataString);
    http.Free;
    ARequest.Free;
  end;
end;

I am facing 401 unauthorized. Please check my ques @A1rPun. Please help me

moskito-x
  • 11,832
  • 5
  • 47
  • 60
vikas
  • 73
  • 10

1 Answers1

0

Thank you for providing a full code sample. I've seen you commented on your previous question that you use indy 10 in Delphi XE2.

The problem could be that there is something wrong with the SSL authentication within indy 10. Full explanation here What you can do is trying to get a stable version of the indy components.

The code seem to have no errors except for a couple of resources that could have been freed ;).

I really hope when updating your indy your problem will solve.

Community
  • 1
  • 1
A1rPun
  • 16,287
  • 7
  • 57
  • 90
  • I have tried updated indy(10.5.8) from svn Revision(4771). Still facing same issue 401 Unauthorized. Please help me @A1rPun. I am trying alot of solutions. – vikas Jun 20 '12 at 13:47
  • Hi @A1rPun did you not get my Ques. Please tell me something.I am getting own status in XML but not doing post Please help me. If You have some time Then I want to chat with you.So Please contact me..... – vikas Jun 22 '12 at 13:02
  • Please Anyone help me. Till Now I am not posting xml with header. All time facing 401 error. My response is 401 1340895781480 BYE79V9AXB 0 [unauthorized]. OAU:2j71bnoc4x5f|3b2f15bd-a2c8-474f-ae05-541dd9b76468|*01|*01:1340895531:PxhePw0/XHdvPz9cP3k/Px5WXz8= Please help me ............ – vikas Jun 28 '12 at 15:04