Upload files with php curl and api - 403 Forbidden

Hello,
I am trying to upload a file to my repository.
The personal token is generated and gave permissions - repos all.
I have read: How to upload a file using GitHub API
The error that I get is: The requested URL returned error: 403 Forbidden

here is my code:

$file_git = "test.jpg";
$data_git = array(
'sha'=>sha1_file($file_git),
'message'=>'test upload',
'content'=> base64_encode(file_get_contents($file_git)),
'committer'=> array(
  'name'=>'demo',
  'email' => 'demo@demov.com'
),
);
$data_string_git = json_encode($data_git); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/MYREPO/contents/test.jpg");  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string_git);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Accept: application/vnd.github.v3+json',
  'Content-Type: application/json',
  'Authorization: token mytoken'
  )
);   
$result = curl_exec($ch);
if (curl_errno($ch)) {
  print_r(curl_error($ch));
}
curl_close($ch);  
print_r($result);

I have tried with OWNER/REPO - in the URL , Authorization: myuser:mytoken as well,
Error is the same.

Please let me know if I have to configure something else.
it is a private repository.

thank you!

1 Like